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-hackers(at)postgresql(dot)org>
Subject: about the magic(?) of palloc() and variable-length user-defined data type
Date: 2001-11-14 20:22:14
Message-ID: 000701c16d4a$0c722820$15c65882@cs.man.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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() which is
called by palloc()?
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

Browse pgsql-hackers by date

  From Date Subject
Next Message Seung Hyun Jeong 2001-11-14 20:23:57 about the magic(?) of palloc() and variable-length user-defined data type
Previous Message Bruce Momjian 2001-11-14 18:07:14 Re: Open items