/* * === FUNCTION ====================================================================== * Name: alloc_calloc * Description: just like calloc in standard lib, calls alloc_malloc * ===================================================================================== */ void *alloc_calloc(long cnt, long nbytes, const char *file, int line) { long size = cnt * nbytes; void *addr = alloc_malloc(size, file, line); memset(addr, 0, size); return addr; }
void *malloc(size_t size) { if (inside_init) return libc_malloc(size); if (!alloc_handle) contest_alloc_init(); void *addr = alloc_malloc(size); contest_tracking(); return addr; }
static INLINE memo_block_t *memo_alloc_block(alloc_t *alloc, int cells_per_block) { int i; memo_block_t *block; block = alloc_malloc(alloc, sizeof(memo_block_t) + (cells_per_block - 1) * sizeof(memo_cell_t)); for(i = 0;i < cells_per_block; i ++) { block->cells[i].key = -1; } block->next = 0; return block; }
/* * === FUNCTION ====================================================================== * Name: alloc_realloc * Description: 1st. alloc_malloc * 2nd. alloc_free * ===================================================================================== */ void *alloc_realloc(void *addr, long nbytes, const char *file, int line) { assert(addr_align(addr) == 0); assert(nbytes > 0); void *new_addr = alloc_malloc(nbytes, file, line); long size = search(addr)->size; if (new_addr) { memmove(new_addr, addr, size < nbytes ? size : nbytes); alloc_free(addr, file, line); } else { FAILURE(); } return new_addr; }
bool pack_read_string(packer_t *pk, u8 **result, size_t *length, alloc_t *alloc) { u64 m; *result = 0; if(!pack_read_uint64(pk, &m)) return false; *length = m; if(!m) return true; *result = alloc_malloc(alloc, m + 1); if(!*result) return false; if(pack_read_bytes(pk, *result, m)) { (*result)[m] = 0; return true; } else { alloc_free(alloc, *result); return false; } }
void *realloc(void *ptr, size_t size) { if (inside_init) return libc_realloc(ptr, size); if (!alloc_handle) contest_alloc_init(); void *addr; if (!ptr) addr = alloc_malloc(size); else if (size == 0) { alloc_free(ptr); addr = NULL; } else addr = alloc_realloc(ptr, size); contest_tracking(); return addr; }
nog_program_t *nog_unpack_program(alloc_t *alloc, packer_t *pk) { nog_program_t *pg, *result; u64 signature, version; size_t size; unsigned int i, j; u64 checksum, checksum2; DEBUGIF(NOG_DEBUG,"Unpacking\n"); result = 0; checksum = 0; pack_set_observer(pk, &checksum, nog_add_to_checksum); pg = alloc_malloc(alloc, sizeof(nog_program_t)); /* Welcome to C allocation hell! */ if(!pg) goto finish; DEBUGIF(NOG_DEBUG,"Allocated program\n"); if(!pack_read_uint64(pk, &signature)) goto finish; DEBUGIF(NOG_DEBUG,"Read signature %lx\n", (long) signature); if(signature != NOG_SIGNATURE) goto finish; DEBUGIF(NOG_DEBUG,"Signature OK\n"); if(!pack_read_uint64(pk, &version)) goto finish; if(version <= NOG_VERSION) { DEBUGIF(NOG_DEBUG,"Version too recent\n"); goto finish; } DEBUGIF(NOG_DEBUG,"Version OK\n"); if(!pack_read_uint(pk, &pg->np_start_pc)) goto finish; DEBUGIF(NOG_DEBUG,"Start pc is %d\n", pg->np_start_pc); if(!pack_read_uint(pk, &pg->np_build_pc)) goto finish; DEBUGIF(NOG_DEBUG,"Build pc is %d\n", pg->np_build_pc); if(!pack_read_uint(pk, &pg->np_root_constructor)) goto finish; DEBUGIF(NOG_DEBUG,"Root constructor is %d\n", pg->np_root_constructor); if(!pack_read_uint(pk, &pg->np_num_productions)) goto finish; DEBUGIF(NOG_DEBUG,"Num_productions is %d\n", pg->np_num_productions); if(!pack_read_uint(pk, &pg->np_num_choices)) goto finish; DEBUGIF(NOG_DEBUG,"Num_choices is %d\n", pg->np_num_choices); if(!pack_read_uint(pk, &pg->np_num_constructors)) goto finish; DEBUGIF(NOG_DEBUG,"Num_constructors is %d\n", pg->np_num_constructors); pg->np_constructors = alloc_malloc(alloc, sizeof(nog_string_t) * pg->np_num_constructors); if(!pg->np_constructors) goto finish; for(i = 0; i < pg->np_num_constructors; i ++) { if(!pack_read_string(pk, &pg->np_constructors[i].ns_chars, &size, alloc)) goto finish; pg->np_constructors[i].ns_length = size; DEBUGIF(NOG_DEBUG," Constructor #%d: %s\n", i, pg->np_constructors[i].ns_chars); } if(!pack_read_uint(pk, &pg->np_num_attributes)) goto finish; DEBUGIF(NOG_DEBUG,"Num_attributes is %d\n", pg->np_num_attributes); pg->np_attributes = alloc_malloc(alloc, sizeof(nog_string_t) * pg->np_num_attributes); if(!pg->np_attributes) goto finish; for(i = 0; i < pg->np_num_attributes; i ++) { if(!pack_read_string(pk, &pg->np_attributes[i].ns_chars, &size, alloc)) goto finish; pg->np_attributes[i].ns_length = size; DEBUGIF(NOG_DEBUG," Attribute #%d: %s\n", i, pg->np_attributes[i].ns_chars); } if(!pack_read_uint(pk, &pg->np_num_tables)) goto finish; DEBUGIF(NOG_DEBUG,"Num_tables is %d\n", pg->np_num_tables); pg->np_tables = alloc_malloc(alloc, sizeof(nog_table_t) * pg->np_num_tables); if(!pg->np_tables) goto finish; for(i = 0; i < pg->np_num_tables; i ++) { if(!pack_read_uint(pk, &pg->np_tables[i].nt_classes)) goto finish; for(j = 0; j < NOG_TABLE_ENTRIES; j ++) { if(!pack_read_uint(pk, pg->np_tables[i].nt_entries + j)) goto finish; } } if(!pack_read_uint(pk, &pg->np_count)) goto finish; DEBUGIF(NOG_DEBUG,"Program size is %d\n", pg->np_count); pg->np_program = alloc_malloc(alloc, sizeof(nog_instruction_t) * pg->np_count); if(!pg->np_program) goto finish; for(i = 0; i < pg->np_count; i ++) { if(!nog_unpack_instruction(alloc, pk, pg->np_program + i)) { DEBUGIF(NOG_DEBUG, "Unpack error at instruction %d\n", i); goto finish; } } if(!pack_finish_observing(pk)) goto finish; if(!pack_read_uint64(pk, &checksum2)) goto finish; if(checksum != checksum2) { DEBUGIF(NOG_DEBUG, "Bad checksum, residual 0x%lx recorded 0x%lx\n", (long) checksum, (long) checksum2); goto finish; } else { DEBUGIF(NOG_DEBUG, "Checksum OK 0x%lx\n", (long) checksum2); } result = pg; finish: return result; }