Skip site navigation (1) Skip section navigation (2)

about the magic(?) of palloc() and variable-length user-defined data type

From: "Seung Hyun Jeong" <jeongs(at)cs(dot)man(dot)ac(dot)uk>
To: <pgsql-interfaces(at)postgresql(dot)org>
Subject: about the magic(?) of palloc() and variable-length user-defined data type
Date: 2001-11-14 20:23:57
Message-ID: 000d01c16d4a$49d7ee20$15c65882@cs.man.ac.uk (view raw or flat)
Thread:
Lists: pgsql-hackerspgsql-interfaces
Hi.

Let me ask you somthing about the magic of palloc()
- at least for me, it looks like magic, and variable-length user-defined
data types.
Hopefully, there is anyone who can help me... I am really struggling to
grasp PostgreSQL.

According to chapter 4 of the Programmer's Guide, user-defined data types
can
have one of these internal formats.

- pass by value, fixed-length
- pass by reference, fixed-length
- pass by reference, variable-length

I am trying to define an user-defined data type corresponding to the third
case,
but let me use the example in the manual.
In the manual, I found this example:

typedef struct {
   int4 length;
   char data[1];
} text;


1) then, my first question is about the example for coding as follows:

char buffer[40];
....
text *destination = (text *) palloc(VARHDRSZ + 40);
....

I cannot understand this mechanism... it looks like magic to me..
If it is like the following, I can see it:

text *destination = (text *) palloc(sizeof(text));
destination->data = (char *) palloc(40);

In this case, there still remains a question, how can I allocate a bunch of
memory
to "char data[1]" - it is an array data type, not pointer...

I tried to see the source code, and found it just call MemoryContextAlloc(),
but
MemoryContextAlloc() just has an empty function body.

Is there anyone who can tell me the magic of the palloc()?
And which header file do I need to include for palloc()?

2) And my sencond question is how to create such variable-lenth data type.

In Chapter 5 of the Programmer's Guide, the example for a fixed-length
user-defined
data types, 'Complex', is:

....
(let's assume input and output function for a new user-defined data type
have been
created.)

CREATE TYPE complex (
   internallength = 16,
   input = complex_in,
   output = complex_out
);

But, I have no idea what I need to set to 'internallength' for
variable-length data types.


3) My last curiosity is about linking problem.

If I want to make a stand-alone program which call internal functions,
especially palloc(),
to which library I need to link my program?
I started this attempt to find an answer for my first question, but now I am
very
curious about it, becuase I realize that I cannot use the client libraries
in ..../pgsql/lib
such as libpgeasy and libpg, instead I suppose I need to link my stand-alone
program
to server libraries. But the problem is server libraries are shared
libraries and I have
no idea about the mechanism.
Which shared library has the reference to MemoryContextAlloc()?
And is there anything on which I need to take care to link my stand-alone
program to such
shared library? Is it perhaps impossible?

Anyway, thank you for reading my long e-mail.
Cheers.

From someone who is trying to love PostgreSQL.




Responses

pgsql-hackers by date

Next:From: Serguei MokhovDate: 2001-11-14 20:38:09
Subject: Re: Open items
Previous:From: Seung Hyun JeongDate: 2001-11-14 20:22:14
Subject: about the magic(?) of palloc() and variable-length user-defined data type

pgsql-interfaces by date

Next:From: Florence LaiDate: 2001-11-14 21:24:01
Subject: postgresql and MS server 2000
Previous:From: Alexaki SofiaDate: 2001-11-14 17:36:00
Subject: problem with odbc include files

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group