PgFns.txt

text/plain

Filename: PgFns.txt
Type: text/plain
Part: 1
Message: Re: Access violation from palloc, Visual Studio 2005, C-language function

#define BUILDING_DLL
#include <postgres.h>

#include <fmgr.h>
#include <funcapi.h>


PG_FUNCTION_INFO_V1(add_one);

extern __declspec(dllexport)
Datum
add_one(PG_FUNCTION_ARGS)
{
    int32   arg = PG_GETARG_INT32(0);

    PG_RETURN_INT32(arg + 30);

}

PG_FUNCTION_INFO_V1(copytext);

extern __declspec(dllexport)
Datum
copytext(PG_FUNCTION_ARGS)
{
    text     *t = PG_GETARG_TEXT_P(0);
    /*
     * VARSIZE is the total size of the struct in bytes.
     */
    text     *new_t = (text *) palloc(VARSIZE(t));
    SET_VARSIZE(new_t, VARSIZE(t));
    /*
     * VARDATA is a pointer to the data region of the struct.
     */
    memcpy((void *) VARDATA(new_t), /* destination */
           (void *) VARDATA(t),     /* source */
           VARSIZE(t) - VARHDRSZ);  /* how many bytes */
    PG_RETURN_TEXT_P(new_t);
}





#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif