int inv_seek(struct lobj *obj_desc, int offset, int whence) { ASSERT(PTR_VALID(obj_desc)); switch (whence) { case SEEK_SET: if (offset < 0) elog(ERROR, "invalid seek offset: %d", offset); obj_desc->offset = offset; break; case SEEK_CUR: if (offset < 0 && obj_desc->offset < ((uint32) (-offset))) elog(ERROR, "invalid seek offset: %d", offset); obj_desc->offset += offset; break; case SEEK_END: { uint32 size = inv_getsize(obj_desc); if (offset < 0 && size < ((uint32) (-offset))) elog(ERROR, "invalid seek offset: %d", offset); obj_desc->offset = size + offset; } break; default: elog(ERROR, "invalid whence: %d", whence); } return obj_desc->offset; }
int inv_tell(struct lobj *obj_desc) { ASSERT(PTR_VALID(obj_desc)); return obj_desc->offset; }
/* * Closes a large object descriptor previously made by inv_open(), and * releases the long-term memory used by it. */ void inv_close(struct lobj *obj_desc) { ASSERT(PTR_VALID(obj_desc)); if (obj_desc->snapshot != snap_now) unregister_snap_res(obj_desc->snapshot, top_xact_resource); pfree(obj_desc); }
/*------------------------------------------------------------------------- * datum_size * * Find the "real" size of a datum, given the datum value, * whether it is a "by value", and the declared type length. * * This is essentially an out-of-line version of the ATTR_ADD_LENGTH_DATUM() * macro in access/tup_macros.h. We do a tad more error checking though. *------------------------------------------------------------------------- */ size_t datum_size(datum_t value, bool typByVal, int typLen) { size_t size; if (typByVal) { /* Pass-by-value types are always fixed-length */ ASSERT(typLen > 0 && typLen <= sizeof(datum_t)); size = (size_t) typLen; } else { if (typLen > 0) { /* Fixed-length pass-by-ref type */ size = (size_t) typLen; } else if (typLen == -1) { /* It is a varlena datatype */ struct vla *s; s = (struct vla *)D_TO_PTR(value); if (!PTR_VALID(s)) ereport(ERROR, ( errcode(E_DATA_EXCEPTION), errmsg("invalid datum_t pointer"))); size = (size_t) VLA_SZ_ANY(s); } else if (typLen == -2) { /* It is a cstring datatype */ char *s = (char *)D_TO_PTR(value); if (!PTR_VALID(s)) ereport(ERROR, ( errcode(E_DATA_EXCEPTION), errmsg("invalid datum_t pointer"))); size = (size_t) (strlen(s) + 1); } else { elog(ERROR, "invalid typLen: %d", typLen); size = 0; /* keep compiler quiet */ } } return size; }
GC_USER_FUNC GC_CAP char * copy_string (GC_CAP const char * str) { size_t len = cstrlen(str)+1; GC_CAP char * copy = GC_INVALID_PTR(); GC_STORE_CAP(copy, ml_malloc(len)); if (!PTR_VALID(copy)) { fprintf(stderr, "copy_string(): out of memory\n"); exit(1); } cmemcpy(copy, str, len); return copy; }
GC_USER_FUNC void lex_read_file (GC_CAP const char * name) { lex_state.num_tokens = 0; FILE * file = fopen((const char *) name, "rb"); if (!file) { fprintf(stderr, "could not open file %s\n", (const char *) name); exit(1); } // A very inefficient way of reading a file, designed to stress the collector. lex_state.max = 0; lex_state.file = GC_INVALID_PTR(); lex_state.index = 0; char c; while (fread(&c, 1, 1, file) == 1) { lex_state.max++; GC_CAP char * tmp = GC_INVALID_PTR(); GC_STORE_CAP(tmp, ml_malloc(lex_state.max)); if (!PTR_VALID(tmp)) { fprintf(stderr, "out of memory reading file %s\n", (const char *) name); exit(1); } cmemset(tmp, 0, lex_state.max); if (PTR_VALID(lex_state.file)) { cmemcpy(tmp, lex_state.file, lex_state.max-1); } GC_STORE_CAP(lex_state.file, tmp); lex_state.file[lex_state.max-1] = c; } fclose(file); }
GC_USER_FUNC void lex_read_string (GC_CAP const char * str) { lex_state.num_tokens = 0; lex_state.file = GC_INVALID_PTR(); GC_STORE_CAP(lex_state.file, copy_string(str)); if (!PTR_VALID(lex_state.file)) { fprintf(stderr, "lex_read_string: out of memory\n"); exit(1); } //GC_debug_track_allocated(lex_state.file, "lex_state file"); lex_state.index = 0; lex_state.max = cstrlen(str); }
/* * Determine size of a large object * * NOTE: LOs can contain gaps, just like Unix files. We actually return * the offset of the last byte + 1. */ static uint32 inv_getsize(struct lobj *obj_desc) { uint32 lastbyte = 0; struct scankey skey[1]; struct sys_scan* sd; struct heap_tuple* tuple; ASSERT(PTR_VALID(obj_desc)); open_lo_relation(); scankey_init(&skey[0], Anum_pg_largeobject_loid, BT_EQ_STRAT_NR, F_OIDEQ, OID_TO_D(obj_desc->id)); sd = systable_beginscan_ordered(lo_heap_r, lo_index_r, obj_desc->snapshot, 1, skey); /* * Because the pg_largeobject index is on both loid and pageno, but we * constrain only loid, a backwards scan should visit all pages of the * large object in reverse pageno order. So, it's sufficient to examine * the first valid tuple (== last valid page). */ tuple = systable_getnext_ordered(sd, BACKWARD_SCANDIR); if (HT_VALID(tuple)) { Form_pg_largeobject data; bytea* datafield; bool pfreeit; if (HT_HAS_NULLS(tuple)) /* paranoia */ elog(ERROR, "null field found in pg_largeobject"); data = (Form_pg_largeobject) GET_STRUCT(tuple); datafield = &(data->data); /* see note at top of file */ pfreeit = false; if (VLA_EXTENDED(datafield)) { datafield = (bytea *) heap_tuple_untoast_attr((struct vla *) datafield); pfreeit = true; } lastbyte = data->pageno * LO_BLK_SIZE + getbytealen(datafield); if (pfreeit) pfree(datafield); } systable_endscan_ordered(sd); return lastbyte; }
// Builds Huffman tree. Called with the first 8 bits loaded from input stream void THuffmannTree::BuildTree(unsigned int nCmpType) { unsigned long maxByte; // [ESP+10] - The greatest character found in table THTreeItem ** itemPtr; // [ESP+14] - Pointer to Huffman tree item pointer array unsigned char * byteArray; // [ESP+1C] - Pointer to unsigned char in Table1502A630 THTreeItem * child1; unsigned long i; // egcs in linux doesn't like multiple for loops without an explicit i // Loop while pointer has a valid value while(PTR_VALID(pLast)) // ESI - Last entry { THTreeItem * temp; // EAX if(pLast->next != NULL) // ESI->next pLast->RemoveItem(); // EDI = &offs3054 pItem3058 = PTR_PTR(&pItem3054); // [EDI+4] pLast->prev = pItem3058; // EAX temp = PTR_PTR(&pItem3054)->GetPrevItem(PTR_INT(&pItem3050)); temp->next = pLast; pItem3054 = pLast; } // Clear all pointers in HTree item array memset(items306C, 0, sizeof(items306C)); maxByte = 0; // Greatest character found init to zero. itemPtr = (THTreeItem **)&items306C; // Pointer to current entry in HTree item pointer array // Ensure we have low 8 bits only nCmpType &= 0xFF; byteArray = Table1502A630 + nCmpType * 258; // EDI also for(i = 0; i < 0x100; i++, itemPtr++) { THTreeItem * item = pItem3058; // Item to be created THTreeItem * pItem3 = pItem3058; unsigned char oneByte = byteArray[i]; // Skip all the bytes which are zero. if(byteArray[i] == 0) continue; // If not valid pointer, take the first available item in the array if(PTR_INVALID_OR_NULL(item)) item = &items0008[nItems++]; // Insert this item as the top of the tree InsertItem(&pItem305C, item, SWITCH_ITEMS, NULL); item->parent = NULL; // Invalidate child and parent item->child = NULL; *itemPtr = item; // Store pointer into pointer array item->dcmpByte = i; // Store counter item->byteValue = oneByte; // Store byte value if(oneByte >= maxByte) { maxByte = oneByte; continue; } // Find the first item which has byte value greater than current one byte if(PTR_VALID(pItem3 = pLast)) // EDI - Pointer to the last item { // 15006AF7 if(pItem3 != NULL) { do // 15006AFB { if(pItem3->byteValue >= oneByte) goto _15006B09; pItem3 = pItem3->prev; } while(PTR_VALID(pItem3)); } } pItem3 = NULL; // 15006B09 _15006B09: if(item->next != NULL) item->RemoveItem(); // 15006B15 if(pItem3 == NULL) pItem3 = PTR_PTR(&pFirst); // 15006B1F item->next = pItem3->next; item->prev = pItem3->next->prev; pItem3->next->prev = item; pItem3->next = item; } // 15006B4A for(; i < 0x102; i++) { THTreeItem ** itemPtr = &items306C[i]; // EDI // 15006B59 THTreeItem * item = pItem3058; // ESI if(PTR_INVALID_OR_NULL(item)) item = &items0008[nItems++]; InsertItem(&pItem305C, item, INSERT_ITEM, NULL); // 15006B89 item->dcmpByte = i; item->byteValue = 1; item->parent = NULL; item->child = NULL; *itemPtr++ = item; } // 15006BAA if(PTR_VALID(child1 = pLast)) // EDI - last item (first child to item { THTreeItem * child2; // EBP THTreeItem * item; // ESI // 15006BB8 while(PTR_VALID(child2 = child1->prev)) { if(PTR_INVALID_OR_NULL(item = pItem3058)) item = &items0008[nItems++]; // 15006BE3 InsertItem(&pItem305C, item, SWITCH_ITEMS, NULL); // 15006BF3 item->parent = NULL; item->child = NULL; //EDX = child2->byteValue + child1->byteValue; //EAX = child1->byteValue; //ECX = maxByte; // The greatest character (0xFF usually) item->byteValue = child1->byteValue + child2->byteValue; // 0x02 item->child = child1; // Prev item in the child1->parent = item; child2->parent = item; // EAX = item->byteValue; if(item->byteValue >= maxByte) maxByte = item->byteValue; else { THTreeItem * pItem2 = child2->prev; // EDI // 15006C2D while(PTR_VALID(pItem2)) { if(pItem2->byteValue >= item->byteValue) goto _15006C3B; pItem2 = pItem2->prev; } pItem2 = NULL; _15006C3B: if(item->next != 0) { THTreeItem * temp4 = item->GetPrevItem(-1); temp4->next = item->next; // The first item changed item->next->prev = item->prev; // First->prev changed to negative value item->next = NULL; item->prev = NULL; } // 15006C62 if(pItem2 == NULL) pItem2 = PTR_PTR(&pFirst); item->next = pItem2->next; // Set item with 0x100 byte value item->prev = pItem2->next->prev; // Set item with 0x17 byte value pItem2->next->prev = item; // Changed prev of item with pItem2->next = item; } // 15006C7B if(PTR_INVALID_OR_NULL(child1 = child2->prev)) break; } } // 15006C88 offs0004 = 1; }
void inv_truncate(struct lobj *obj_desc, int len) { int32 pageno = (int32) (len / LO_BLK_SIZE); int off; struct scankey skey[2]; struct sys_scan* sd; struct heap_tuple* oldtuple; Form_pg_largeobject olddata; struct { bytea hdr; char data[LO_BLK_SIZE]; /* make struct big enough */ int32 align_it;/* ensure struct is aligned well enough */ } workbuf; char* workb = VLA_DATA(&workbuf.hdr); struct heap_tuple* newtup; datum_t values[Natts_pg_largeobject]; bool nulls[Natts_pg_largeobject]; bool replace[Natts_pg_largeobject]; CatalogIndexState indstate; ASSERT(PTR_VALID(obj_desc)); /* enforce writability because snapshot is probably wrong otherwise */ if ((obj_desc->flags & IFS_WRLOCK) == 0) ereport(ERROR, ( errcode(E_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("large object %u was not opened for writing", obj_desc->id))); /* check existence of the target largeobject */ if (!large_obj_exists(obj_desc->id)) ereport(ERROR, ( errcode(E_UNDEFINED_OBJECT), errmsg("large object %u was already dropped", obj_desc->id))); open_lo_relation(); indstate = cat_open_indexes(lo_heap_r); /* * Set up to find all pages with desired loid and pageno >= target */ scankey_init(&skey[0], Anum_pg_largeobject_loid, BT_EQ_STRAT_NR, F_OIDEQ, OID_TO_D(obj_desc->id)); scankey_init(&skey[1], Anum_pg_largeobject_pageno, BT_GE_STRAT_NR, F_INT4GE, INT32_TO_D(pageno)); sd = systable_beginscan_ordered(lo_heap_r, lo_index_r, obj_desc->snapshot, 2, skey); /* * If possible, get the page the truncation point is in. The truncation * point may be beyond the end of the LO or in a hole. */ olddata = NULL; if ((oldtuple = systable_getnext_ordered(sd, FORWARD_SCANDIR)) != NULL) { if (HT_HAS_NULLS(oldtuple)) /* paranoia */ elog(ERROR, "null field found in pg_largeobject"); olddata = (Form_pg_largeobject) GET_STRUCT(oldtuple); ASSERT(olddata->pageno >= pageno); } /* * If we found the page of the truncation point we need to truncate the * data in it. Otherwise if we're in a hole, we need to create a page to * mark the end of data. */ if (olddata != NULL && olddata->pageno == pageno) { /* First, load old data into workbuf */ bytea* datafield = &(olddata->data); /* see note at top of file */ bool pfreeit = false; int pagelen; if (VLA_EXTENDED(datafield)) { datafield = (bytea *) heap_tuple_untoast_attr((struct vla *) datafield); pfreeit = true; } pagelen = getbytealen(datafield); ASSERT(pagelen <= LO_BLK_SIZE); memcpy(workb, VLA_DATA(datafield), pagelen); if (pfreeit) pfree(datafield); /* * Fill any hole */ off = len % LO_BLK_SIZE; if (off > pagelen) pg_memset(workb + pagelen, 0, off - pagelen); /* compute length of new page */ VLA_SET_SZ_STND(&workbuf.hdr, off + VAR_HDR_SZ); /* * Form and insert updated tuple */ memset(values, 0, sizeof(values)); memset(nulls, false, sizeof(nulls)); memset(replace, false, sizeof(replace)); values[Anum_pg_largeobject_data - 1] = PTR_TO_D(&workbuf); replace[Anum_pg_largeobject_data - 1] = true; newtup = heap_modify_tuple(oldtuple, REL_DESC(lo_heap_r), values, nulls, replace); simple_heap_update(lo_heap_r, &newtup->t_self, newtup); cat_index_insert(indstate, newtup); heap_free_tuple(newtup); } else { /* * If the first page we found was after the truncation point, we're in * a hole that we'll fill, but we need to delete the later page * because the loop below won't visit it again. */ if (olddata != NULL) { ASSERT(olddata->pageno > pageno); simple_heap_delete(lo_heap_r, &oldtuple->t_self); } /* * Write a brand new page. * * Fill the hole up to the truncation point */ off = len % LO_BLK_SIZE; if (off > 0) pg_memset(workb, 0, off); /* compute length of new page */ VLA_SET_SZ_STND(&workbuf.hdr, off + VAR_HDR_SZ); /* * Form and insert new tuple */ memset(values, 0, sizeof(values)); memset(nulls, false, sizeof(nulls)); values[Anum_pg_largeobject_loid - 1] = OID_TO_D(obj_desc->id); values[Anum_pg_largeobject_pageno - 1] = INT32_TO_D(pageno); values[Anum_pg_largeobject_data - 1] = PTR_TO_D(&workbuf); newtup = heap_form_tuple(lo_heap_r->rd_att, values, nulls); simple_heap_insert(lo_heap_r, newtup); cat_index_insert(indstate, newtup); heap_free_tuple(newtup); } /* * Delete any pages after the truncation point. If the initial search * didn't find a page, then of course there's nothing more to do. */ if (olddata != NULL) { while ((oldtuple = systable_getnext_ordered(sd, FORWARD_SCANDIR)) != NULL) { simple_heap_delete(lo_heap_r, &oldtuple->t_self); } } systable_endscan_ordered(sd); cat_close_indexes(indstate); /* * Advance command counter so that tuple updates will be seen by later * large-object operations in this transaction. */ cmd_count_incr(); }
int inv_write(struct lobj *obj_desc, const char *buf, int nbytes) { int nwritten = 0; int n; int off; int len; int32 pageno = (int32) (obj_desc->offset / LO_BLK_SIZE); struct scankey skey[2]; struct sys_scan * sd; struct heap_tuple * oldtuple; Form_pg_largeobject olddata; bool neednextpage; bytea* datafield; bool pfreeit; struct { bytea hdr; char data[LO_BLK_SIZE]; /* make struct big enough */ int32 align_it; /* ensure struct is aligned well enough */ } workbuf; char* workb = VLA_DATA(&workbuf.hdr); struct heap_tuple* newtup; datum_t values[Natts_pg_largeobject]; bool nulls[Natts_pg_largeobject]; bool replace[Natts_pg_largeobject]; CatalogIndexState indstate; ASSERT(PTR_VALID(obj_desc)); ASSERT(buf != NULL); /* enforce writability because snapshot is probably wrong otherwise */ if ((obj_desc->flags & IFS_WRLOCK) == 0) ereport(ERROR, ( errcode(E_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("large object %u was not opened for writing", obj_desc->id))); /* check existence of the target largeobject */ if (!large_obj_exists(obj_desc->id)) ereport(ERROR, ( errcode(E_UNDEFINED_OBJECT), errmsg("large object %u was already dropped", obj_desc->id))); if (nbytes <= 0) return 0; open_lo_relation(); indstate = cat_open_indexes(lo_heap_r); scankey_init(&skey[0], Anum_pg_largeobject_loid, BT_EQ_STRAT_NR, F_OIDEQ, OID_TO_D(obj_desc->id)); scankey_init(&skey[1], Anum_pg_largeobject_pageno, BT_GE_STRAT_NR, F_INT4GE, INT32_TO_D(pageno)); sd = systable_beginscan_ordered(lo_heap_r, lo_index_r, obj_desc->snapshot, 2, skey); oldtuple = NULL; olddata = NULL; neednextpage = true; while (nwritten < nbytes) { /* * If possible, get next pre-existing page of the LO. We expect the * indexscan will deliver these in order --- but there may be holes. */ if (neednextpage) { if ((oldtuple = systable_getnext_ordered(sd, FORWARD_SCANDIR)) != NULL) { if (HT_HAS_NULLS(oldtuple)) /* paranoia */ elog(ERROR, "null field found in pg_largeobject"); olddata = (Form_pg_largeobject) GET_STRUCT(oldtuple); ASSERT(olddata->pageno >= pageno); } neednextpage = false; } /* * If we have a pre-existing page, see if it is the page we want to * write, or a later one. */ if (olddata != NULL && olddata->pageno == pageno) { /* * Update an existing page with fresh data. * * First, load old data into workbuf */ datafield = &(olddata->data); /* see note at top of file */ pfreeit = false; if (VLA_EXTENDED(datafield)) { datafield = (bytea *) heap_tuple_untoast_attr((struct vla *) datafield); pfreeit = true; } len = getbytealen(datafield); ASSERT(len <= LO_BLK_SIZE); memcpy(workb, VLA_DATA(datafield), len); if (pfreeit) pfree(datafield); /* * Fill any hole */ off = (int)(obj_desc->offset % LO_BLK_SIZE); if (off > len) pg_memset(workb + len, 0, off - len); /* * Insert appropriate portion of new data */ n = LO_BLK_SIZE - off; n = (n <= (nbytes - nwritten))? n : (nbytes - nwritten); memcpy(workb + off, buf + nwritten, n); nwritten += n; obj_desc->offset += n; off += n; /* compute valid length of new page */ len = (len >= off) ? len : off; VLA_SET_SZ_STND(&workbuf.hdr, len + VAR_HDR_SZ); /* * Form and insert updated tuple */ memset(values, 0, sizeof(values)); memset(nulls, false, sizeof(nulls)); memset(replace, false, sizeof(replace)); values[Anum_pg_largeobject_data - 1] = PTR_TO_D(&workbuf); replace[Anum_pg_largeobject_data - 1] = true; newtup = heap_modify_tuple(oldtuple, REL_DESC(lo_heap_r), values, nulls, replace); simple_heap_update(lo_heap_r, &newtup->t_self, newtup); cat_index_insert(indstate, newtup); heap_free_tuple(newtup); /* * We're done with this old page. */ oldtuple = NULL; olddata = NULL; neednextpage = true; } else { /* * Write a brand new page. * * First, fill any hole */ off = (int)(obj_desc->offset % LO_BLK_SIZE); if (off > 0) pg_memset(workb, 0, off); /* * Insert appropriate portion of new data */ n = LO_BLK_SIZE - off; n = (n <= (nbytes - nwritten))? n : (nbytes - nwritten); memcpy(workb + off, buf + nwritten, n); nwritten += n; obj_desc->offset += n; /* compute valid length of new page */ len = off + n; VLA_SET_SZ_STND(&workbuf.hdr, len + VAR_HDR_SZ); /* * Form and insert updated tuple */ memset(values, 0, sizeof(values)); memset(nulls, false, sizeof(nulls)); values[Anum_pg_largeobject_loid - 1] = OID_TO_D(obj_desc->id); values[Anum_pg_largeobject_pageno - 1] = INT32_TO_D(pageno); values[Anum_pg_largeobject_data - 1] = PTR_TO_D(&workbuf); newtup = heap_form_tuple(lo_heap_r->rd_att, values, nulls); simple_heap_insert(lo_heap_r, newtup); cat_index_insert(indstate, newtup); heap_free_tuple(newtup); } pageno++; } systable_endscan_ordered(sd); cat_close_indexes(indstate); /* * Advance command counter so that my tuple updates will be seen by * later large-object operations in this transaction. */ cmd_count_incr(); return nwritten; }
int inv_read(struct lobj *obj_desc, char *buf, int nbytes) { int nread = 0; int n; int off; int len; int32 pageno = (int32) (obj_desc->offset / LO_BLK_SIZE); uint32 pageoff; struct scankey skey[2]; struct sys_scan* sd; struct heap_tuple* tuple; ASSERT(PTR_VALID(obj_desc)); ASSERT(buf != NULL); if (nbytes <= 0) return 0; open_lo_relation(); scankey_init(&skey[0], Anum_pg_largeobject_loid, BT_EQ_STRAT_NR, F_OIDEQ, OID_TO_D(obj_desc->id)); scankey_init(&skey[1], Anum_pg_largeobject_pageno, BT_GE_STRAT_NR, F_INT4GE, INT32_TO_D(pageno)); sd = systable_beginscan_ordered(lo_heap_r, lo_index_r, obj_desc->snapshot, 2, skey); while ((tuple = systable_getnext_ordered(sd, FORWARD_SCANDIR)) != NULL) { Form_pg_largeobject data; bytea* datafield; bool pfreeit; if (HT_HAS_NULLS(tuple)) /* paranoia */ elog(ERROR, "null field found in pg_largeobject"); data = (Form_pg_largeobject) GET_STRUCT(tuple); /* * We expect the indexscan will deliver pages in order. However, * there may be missing pages if the LO contains unwritten "holes". We * want missing sections to read out as zeroes. */ pageoff = ((uint32) data->pageno) * LO_BLK_SIZE; if (pageoff > obj_desc->offset) { n = pageoff - obj_desc->offset; n = (n <= (nbytes - nread)) ? n : (nbytes - nread); pg_memset(buf + nread, 0, n); nread += n; obj_desc->offset += n; } if (nread < nbytes) { ASSERT(obj_desc->offset >= pageoff); off = (int) (obj_desc->offset - pageoff); ASSERT(off >= 0 && off < LO_BLK_SIZE); datafield = &(data->data); /* see note at top of file */ pfreeit = false; if (VLA_EXTENDED(datafield)) { datafield = (bytea *) heap_tuple_untoast_attr((struct vla *) datafield); pfreeit = true; } len = getbytealen(datafield); if (len > off) { n = len - off; n = (n <= (nbytes - nread)) ? n : (nbytes - nread); memcpy(buf + nread, VLA_DATA(datafield) + off, n); nread += n; obj_desc->offset += n; } if (pfreeit) pfree(datafield); } if (nread >= nbytes) break; } systable_endscan_ordered(sd); return nread; }
/* ---------------------------------------------------------------- * procedure_create * * Note: allParameterTypes, parameterModes, parameterNames, and proconfig * are either arrays of the proper types or NULL. We declare them Datum, * not "ArrayType *", to avoid importing array.h into pg_proc_fn.h. * ---------------------------------------------------------------- */ oid_t procedure_create( const char *procedureName, oid_t procNamespace, bool replace, bool returnsSet, oid_t returnType, oid_t languageObjectId, oid_t languageValidator, const char *prosrc, const char *probin, bool isAgg, bool isWindowFunc, bool security_definer, bool isStrict, char volatility, oid_vector_s *parameterTypes, datum_t allParameterTypes, datum_t parameterModes, datum_t parameterNames, struct list *parameterDefaults, datum_t proconfig, float4 procost, float4 prorows) { oid_t retval; int parameterCount; int allParamCount; oid_t* allParams; bool genericInParam = false; bool genericOutParam = false; bool internalInParam = false; bool internalOutParam = false; oid_t variadicType = INVALID_OID; oid_t proowner = get_uid(); acl_s* proacl = NULL; struct relation* rel; struct heap_tuple* tup; struct heap_tuple* oldtup; bool nulls[Natts_pg_proc]; datum_t values[Natts_pg_proc]; bool replaces[Natts_pg_proc]; oid_t relid; struct name procname; struct tuple* tupDesc; bool is_update; struct objaddr myself; struct objaddr referenced; int i; /* * sanity checks */ ASSERT(PTR_VALID(prosrc)); parameterCount = parameterTypes->dim1; if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS) { ereport(ERROR, ( errcode(E_TOO_MANY_ARGUMENTS), errmsg_plural("functions cannot have more than %d argument", "functions cannot have more than %d arguments", FUNC_MAX_ARGS, FUNC_MAX_ARGS))); } /* note: the above is correct, we do NOT count output arguments */ if (allParameterTypes != PTR_TO_D(NULL)) { /* * We expect the array to be a 1-D OID array; verify that. We don't * need to use deconstruct_array() since the array data is just going * to look like a C array of OID values. */ array_s *allParamArray; allParamArray = (array_s*) D_TO_PTR(allParameterTypes); allParamCount = ARR_DIMS(allParamArray)[0]; if (ARR_NDIM(allParamArray) != 1 || allParamCount <= 0 || ARR_HASNULL(allParamArray) || ARR_ELEMTYPE(allParamArray) != OIDOID) elog(ERROR, "allParameterTypes is not a 1-D oid_t array"); allParams = (oid_t*) ARR_DATA_PTR(allParamArray); ASSERT(allParamCount >= parameterCount); /* we assume caller got the contents right */ } else { allParamCount = parameterCount; allParams = parameterTypes->values; } /* * Do not allow polymorphic return type unless at least one input argument * is polymorphic. Also, do not allow return type INTERNAL unless at * least one input argument is INTERNAL. */ for (i = 0; i < parameterCount; i++) { switch (parameterTypes->values[i]) { case ANYARRAYOID: case ANYELEMENTOID: case ANYNONARRAYOID: case ANYENUMOID: genericInParam = true; break; case INTERNALOID: internalInParam = true; break; } } if (allParameterTypes != PTR_TO_D(NULL)) { for (i = 0; i < allParamCount; i++) { /* * We don't bother to distinguish input and output params here, so * if there is, say, just an input INTERNAL param then we will * still set internalOutParam. This is OK since we don't really * care. */ switch (allParams[i]) { case ANYARRAYOID: case ANYELEMENTOID: case ANYNONARRAYOID: case ANYENUMOID: genericOutParam = true; break; case INTERNALOID: internalOutParam = true; break; } } } if ((is_polymorphic_type(returnType) || genericOutParam) && !genericInParam) { ereport(ERROR, ( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot determine result data type"), errdetail("A function returning a polymorphic type must have" " at least one polymorphic argument."))); } if ((returnType == INTERNALOID || internalOutParam) && !internalInParam) { ereport(ERROR, ( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("unsafe use of pseudo-type \"internal\""), errdetail("A function returning \"internal\" must have at" " least one \"internal\" argument."))); } /* * don't allow functions of complex types that have the same name as * existing attributes of the type */ if (parameterCount == 1 && OID_VALID(parameterTypes->values[0]) && (relid = typeid_to_relid(parameterTypes->values[0])) != INVALID_OID && get_attnum(relid, procedureName) != INVALID_ATTR_NR) { ereport(ERROR, ( errcode(E_DUPLICATE_COLUMN), errmsg("\"%s\" is already an attribute of type %s", procedureName, format_type_be(parameterTypes->values[0])))); } if (parameterModes != PTR_TO_D(NULL)) { /* * We expect the array to be a 1-D CHAR array; verify that. We don't * need to use deconstruct_array() since the array data is just going * to look like a C array of char values. */ array_s* modesArray; char* modes; modesArray = (array_s *) D_TO_PTR(parameterModes); if (ARR_NDIM(modesArray) != 1 || ARR_DIMS(modesArray)[0] != allParamCount || ARR_HASNULL(modesArray) || ARR_ELEMTYPE(modesArray) != CHAROID) elog(ERROR, "parameterModes is not a 1-D char array"); modes = (char*) ARR_DATA_PTR(modesArray); /* * Only the last input parameter can be variadic; if it is, save its * element type. Errors here are just elog since caller should have * checked this already. */ for (i = 0; i < allParamCount; i++) { switch (modes[i]) { case PROARGMODE_IN: case PROARGMODE_INOUT: if (OID_VALID(variadicType)) elog(ERROR, "variadic parameter must be last"); break; case PROARGMODE_OUT: case PROARGMODE_TABLE: /* okay */ break; case PROARGMODE_VARIADIC: if (OID_VALID(variadicType)) elog(ERROR, "variadic parameter must be last"); switch (allParams[i]) { case ANYOID: variadicType = ANYOID; break; case ANYARRAYOID: variadicType = ANYELEMENTOID; break; default: variadicType = get_element_type(allParams[i]); if (!OID_VALID(variadicType)) elog(ERROR, "variadic parameter is not an array"); break; } break; default: elog(ERROR, "invalid parameter mode '%c'", modes[i]); break; } } } /* * All seems OK; prepare the data to be inserted into pg_proc. */ for (i = 0; i < Natts_pg_proc; ++i) { nulls[i] = false; values[i] = (datum_t) 0; replaces[i] = true; } namestrcpy(&procname, procedureName); values[Anum_pg_proc_proname - 1] = NAME_TO_D(&procname); values[Anum_pg_proc_pronamespace - 1] = OID_TO_D(procNamespace); values[Anum_pg_proc_proowner - 1] = OID_TO_D(proowner); values[Anum_pg_proc_prolang - 1] = OID_TO_D(languageObjectId); values[Anum_pg_proc_procost - 1] = FLOAT4_TO_D(procost); values[Anum_pg_proc_prorows - 1] = FLOAT4_TO_D(prorows); values[Anum_pg_proc_provariadic - 1] = OID_TO_D(variadicType); values[Anum_pg_proc_proisagg - 1] = BOOL_TO_D(isAgg); values[Anum_pg_proc_proiswindow - 1] = BOOL_TO_D(isWindowFunc); values[Anum_pg_proc_prosecdef - 1] = BOOL_TO_D(security_definer); values[Anum_pg_proc_proisstrict - 1] = BOOL_TO_D(isStrict); values[Anum_pg_proc_proretset - 1] = BOOL_TO_D(returnsSet); values[Anum_pg_proc_provolatile - 1] = CHAR_TO_D(volatility); values[Anum_pg_proc_pronargs - 1] = UINT16_TO_D(parameterCount); values[Anum_pg_proc_pronargdefaults - 1] = UINT16_TO_D(list_length(parameterDefaults)); values[Anum_pg_proc_prorettype - 1] = OID_TO_D(returnType); values[Anum_pg_proc_proargtypes - 1] = PTR_TO_D(parameterTypes); if (allParameterTypes != PTR_TO_D(NULL)) values[Anum_pg_proc_proallargtypes - 1] = allParameterTypes; else nulls[Anum_pg_proc_proallargtypes - 1] = true; if (parameterModes != PTR_TO_D(NULL)) values[Anum_pg_proc_proargmodes - 1] = parameterModes; else nulls[Anum_pg_proc_proargmodes - 1] = true; if (parameterNames != PTR_TO_D(NULL)) values[Anum_pg_proc_proargnames - 1] = parameterNames; else nulls[Anum_pg_proc_proargnames - 1] = true; if (parameterDefaults != NIL) values[Anum_pg_proc_proargdefaults - 1] = CStringGetTextDatum( node_to_string(parameterDefaults)); else nulls[Anum_pg_proc_proargdefaults - 1] = true; values[Anum_pg_proc_prosrc - 1] = CStringGetTextDatum(prosrc); if (probin) values[Anum_pg_proc_probin - 1] = CStringGetTextDatum(probin); else nulls[Anum_pg_proc_probin - 1] = true; if (proconfig != PTR_TO_D(NULL)) values[Anum_pg_proc_proconfig - 1] = proconfig; else nulls[Anum_pg_proc_proconfig - 1] = true; /* * proacl will be determined later */ rel = heap_open(ProcedureRelationId, ROW_EXCL_LOCK); tupDesc = REL_DESC(rel); /* Check for pre-existing definition */ oldtup = search_syscache3( PROCNAMEARGSNSP, PTR_TO_D(procedureName), PTR_TO_D(parameterTypes), OID_TO_D(procNamespace)); if (HT_VALID(oldtup)) { /* There is one; okay to replace it? */ Form_pg_proc oldproc; datum_t proargnames; bool isnull; oldproc = (Form_pg_proc) GET_STRUCT(oldtup); if (!replace) { ereport(ERROR, ( errcode(E_DUPLICATE_FUNCTION), errmsg("function \"%s\" already exists with same argument types", procedureName))); } if (!pg_proc_ownercheck(HEAPTUP_OID(oldtup), proowner)) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC, procedureName); /* * Not okay to change the return type of the existing proc, since * existing rules, views, etc may depend on the return type. */ if (returnType != oldproc->prorettype || returnsSet != oldproc->proretset) { ereport(ERROR, ( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot change return type of existing function"), errhint("Use DROP FUNCTION first."))); } /* * If it returns RECORD, check for possible change of record type * implied by OUT parameters */ if (returnType == RECORDOID) { struct tuple* olddesc; struct tuple* newdesc; olddesc = build_function_result_tupdesc_t(oldtup); newdesc = build_function_result_tupdesc_d( allParameterTypes, parameterModes, parameterNames); if (olddesc == NULL && newdesc == NULL) { /* ok, both are runtime-defined RECORDs */ ; } else if (olddesc == NULL || newdesc == NULL || !tupdesc_equal(olddesc, newdesc)) { ereport(ERROR, ( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot change return type of existing function"), errdetail("Row type defined by OUT parameters is different."), errhint("Use DROP FUNCTION first."))); } } /* * If there were any named input parameters, check to make sure the * names have not been changed, as this could break existing calls. We * allow adding names to formerly unnamed parameters, though. */ proargnames = syscache_attr( PROCNAMEARGSNSP, oldtup, Anum_pg_proc_proargnames, &isnull); if (!isnull) { datum_t proargmodes; char** old_arg_names; char** new_arg_names; int n_old_arg_names; int n_new_arg_names; int j; proargmodes = syscache_attr( PROCNAMEARGSNSP, oldtup, Anum_pg_proc_proargmodes, &isnull); if (isnull) proargmodes = PTR_TO_D(NULL); /* just to be sure */ n_old_arg_names = get_func_input_arg_names( proargnames, proargmodes, &old_arg_names); n_new_arg_names = get_func_input_arg_names( parameterNames, parameterModes, &new_arg_names); for (j = 0; j < n_old_arg_names; j++) { if (old_arg_names[j] == NULL) continue; if (j >= n_new_arg_names || new_arg_names[j] == NULL || strcmp(old_arg_names[j], new_arg_names[j]) != 0) { ereport(ERROR,( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot change name of input parameter \"%s\"", old_arg_names[j]), errhint("Use DROP FUNCTION first."))); } } } /* * If there are existing defaults, check compatibility: redefinition * must not remove any defaults nor change their types. (Removing a * default might cause a function to fail to satisfy an existing call. * Changing type would only be possible if the associated parameter is * polymorphic, and in such cases a change of default type might alter * the resolved output type of existing calls.) */ if (oldproc->pronargdefaults != 0) { datum_t proargdefaults; struct list* oldDefaults; struct list_cell* oldlc; struct list_cell* newlc; if (list_length(parameterDefaults) < oldproc->pronargdefaults) { ereport(ERROR, ( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot remove parameter defaults from existing function"), errhint("Use DROP FUNCTION first."))); } proargdefaults = syscache_attr( PROCNAMEARGSNSP, oldtup, Anum_pg_proc_proargdefaults, &isnull); ASSERT(!isnull); oldDefaults = (struct list*) string_to_node( TextD_TO_CSTRING(proargdefaults)); ASSERT(IS_A(oldDefaults, List)); ASSERT(list_length(oldDefaults) == oldproc->pronargdefaults); /* new list can have more defaults than old, advance over 'em */ newlc = list_head(parameterDefaults); for (i = list_length(parameterDefaults) - oldproc->pronargdefaults; i > 0; i--) newlc = lnext(newlc); foreach(oldlc, oldDefaults) { node_n* oldDef; node_n* newDef; oldDef = (node_n*) lfirst(oldlc); newDef = (node_n*) lfirst(newlc); if (expr_type(oldDef) != expr_type(newDef)) { ereport(ERROR,( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot change data type of existing" " parameter default value"), errhint("Use DROP FUNCTION first."))); } newlc = lnext(newlc); } }
GC_USER_FUNC int main (int argc, char ** argv) { #ifdef MEMWATCH mwInit(); mwDoFlush(1); #endif // MEMWATCH ML_START_TIMING(main_time); GC_init(); //ml_print_gc_stats(); printf("Compiled for " #if defined(GC_CHERI) "GC_CHERI" #elif defined(GC_BOEHM) "GC_BOEHM" #elif defined(GC_NONE) "GC_NONE" #elif defined(GC_NOCAP) "GC_NOCAP" #else #error "Define one of GC_CHERI, GC_BOEHM, GC_NONE." #endif // GC_CHERI, GC_BOEHM, GC_NONE " at %s\n", __TIME__ " " __DATE__); //GC_CAP const char * filename = GC_cheri_ptr("ml-tmp", sizeof("ml-tmp")); //lex_read_file(filename); //const char str[] = "(fn x . ((fn x . x) 3) + x) 2"; //const char str[] = "fn f . (fn g. (f (fn a . (g g) a))) (fn g. (f (fn a . (g g) a)))"; //const char str[] = //"((fn f . fn n . if n then n * f (n-1) else 1) (fn n . n)) 5"; // factorial: //const char str[] = // "((fn f . (fn g. (f (fn a . (g g) a))) (fn g. (f (fn a . (g g) a)))) (fn f . fn n . if n then n * f (n-1) else 1)) 6"; // for the benchmark: if (argc < 2) { printf("Need a program argument\n"); return 1; } if (argc < 3) { printf("Need a number argument\n"); return 1; } printf("Program should be evaluating something to do with the number %s\n", argv[2]); int num = atoi(argv[2]); #ifdef GC_CHERI gc_cheri_grow_heap(num); #endif #ifdef GC_BOEHM GC_set_max_heap_size(num >= 1024 ? 350000*(num/1024) : num >= 256 ? 200000 : 65536); #endif /*const char str[] = "((fn f . (fn g. (f (fn a . (g g) a))) (fn g. (f (fn f . (g g) f)))) (fn f . fn n . if n then n + f (n-1) else 1)) "; GC_CAP char * str2 = ml_malloc(sizeof(str)+strlen(argv[1])); cmemcpy(str2, GC_cheri_ptr(str, sizeof(str)), sizeof(str)); cmemcpy(str2+sizeof(str)-1, GC_cheri_ptr(argv[1], strlen(argv[1]+1)), strlen(argv[1]+1)); */ GC_CAP const char * str2 = GC_cheri_ptr(argv[1], strlen(argv[1])+1); unsigned long long before = ml_time(); lex_read_string(str2); printf("program: %s\n\n", (void*)(str2)); /*size_t i; for (i=0; i<lex_state.max; i++) { putchar(((char*)lex_state.file)[i]); } GC_CAP token_t * t; t = lex(); while (t->type != TKEOF) { printf("[%d] (tag=%d alloc=%d) %s\n", ((token_t*)t)->type, (int) GC_cheri_gettag(((token_t*)t)->str), (int) GC_IS_GC_ALLOCATED(((token_t*)t)->str), (char*) ((token_t*)t)->str); GC_malloc(5000); t = lex(); } printf("Finished\n"); return 0;*/ parse_init(); GC_CAP expr_t * expr = GC_INVALID_PTR(); GC_STORE_CAP(expr, parse()); printf("AST:\n"); print_ast(expr); printf("\nDone printing AST\n"); /*printf("collecting loads\n"); ml_collect(); ml_collect(); ml_collect(); ml_collect(); ml_collect(); ml_collect(); ml_collect(); ml_collect(); ml_collect(); ml_collect(); ml_collect(); ml_collect(); ml_collect(); ml_collect(); ml_collect(); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~done collecting loads\n"); GC_debug_print_region_stats(&GC_state.thread_local_region);*/ GC_CAP val_t * val = GC_INVALID_PTR(); int i; for (i=0; i<10; i++) { GC_STORE_CAP(val, eval(expr, GC_INVALID_PTR())); } unsigned long long after = ml_time(); unsigned long long diff = after - before; printf("eval: "); if (!PTR_VALID(val)) printf("(invalid"); else print_val(val); printf("\n\n"); printf("[plotdata] %s %llu\n", argv[2], (unsigned long long) diff); #ifdef GC_CHERI printf("(young) heap size:\n"); printf("[altplotdata] %s %llu\n", argv[2], (unsigned long long) (GC_cheri_getlen(GC_state.thread_local_region.tospace))); #ifdef GC_GENERATIONAL printf("old heap size:\n"); printf("[altplotdataold] %s %llu\n", argv[2], (unsigned long long) (GC_cheri_getlen(GC_state.old_generation.tospace))); #endif // GC_GENERATIONAL #endif // GC_CHERI #ifdef GC_BOEHM printf("[altplotdata] %s %llu\n", argv[2], (unsigned long long) GC_get_heap_size()); #endif // GC_BOEHM ML_STOP_TIMING(main_time, "main()"); ml_print_gc_stats(); ml_print_plot_data(); #ifdef MEMWATCH mwTerm(); #endif // MEMWATCH return 0; }