Thread: C functions and int8?

C functions and int8?

From
Forest Wilkinson
Date:
I have written a few Postgres extension functions in C, and want to modify
some of them to return an int8.  However, I don't see any int8 definition
in postgres.h.  (I have the 7.0.2 RPMs installed.)  How should I
accomplish this?



Re: C functions and int8?

From
Karel Zak
Date:
On Thu, 21 Sep 2000, Forest Wilkinson wrote:

> I have written a few Postgres extension functions in C, and want to modify
> some of them to return an int8.  However, I don't see any int8 definition
> in postgres.h.  (I have the 7.0.2 RPMs installed.)  How should I
> accomplish this?
in the source tree: src/include/c.h
typedef signed char int8;           /* == 8 bits */

... but I not sure if this file is included in the RPM package.

                Karel



Re: C functions and int8?

From
Forest Wilkinson
Date:
That's an int8 meaning "eight bit integer".  I want to work with an int8
meaning "64 bit integer", as described in the docs:
http://www.postgresql.org/users-lounge/docs/7.0/user/datatype.htm#AEN942

So how do I return one of these suckers from a C function?

Forest

On Thu, 21 Sep 2000 10:32:50 +0200 (CEST), Karel Zak wrote:

>
>On Thu, 21 Sep 2000, Forest Wilkinson wrote:
>
>> I have written a few Postgres extension functions in C, and want to modify
>> some of them to return an int8.  However, I don't see any int8 definition
>> in postgres.h.  (I have the 7.0.2 RPMs installed.)  How should I
>> accomplish this?
>
> in the source tree: src/include/c.h
>
> typedef signed char int8;           /* == 8 bits */
>
>
> ... but I not sure if this file is included in the RPM package.
>
>
>                    Karel



Re: C functions and int8?

From
Tom Lane
Date:
Forest Wilkinson <fspam@home.com> writes:
> That's an int8 meaning "eight bit integer".  I want to work with an int8
> meaning "64 bit integer", as described in the docs:
> http://www.postgresql.org/users-lounge/docs/7.0/user/datatype.htm#AEN942
> So how do I return one of these suckers from a C function?

Emulate the code in src/backend/utils/adt/int8.c.

Currently this involves palloc'ing an int8, setting it, and returning
a pointer to it.  For instance, int8 addition is

int64 *
int8pl(int64 *val1, int64 *val2)
{   int64       *result = palloc(sizeof(int64));
   if ((!PointerIsValid(val1)) || (!PointerIsValid(val2)))       return NULL;
   *result = *val1 + *val2;
   return result;
}


In 7.1 it'll be a lot cleaner (IMNSHO anyway ;-)):

Datum
int8pl(PG_FUNCTION_ARGS)
{   int64        val1 = PG_GETARG_INT64(0);   int64        val2 = PG_GETARG_INT64(1);
   PG_RETURN_INT64(val1 + val2);
}

which actually does about the same things under the hood, but you
don't have to sully your hands with 'em ...
        regards, tom lane