static void *ralloc(void *ptr, size_t size, enum m_state state) { //LOG("Request for size change to %zd\n", size); if (size == 0) { shee(ptr); return NULL; } if (ptr == NULL) { return shalloc(size); } struct block *b = B_HEAD(ptr); unsigned long size_pow = get_pow(B_SIZE + size); /* No need for allocation, size would be the same */ if (b->k_size == size_pow) { return ptr; } void *new_ptr; if (state == ZERO) { new_ptr = shcalloc(1, size); } else { new_ptr = shalloc(size); } /* In case of failure, previous data is lost */ if (new_ptr == NULL) { shee(ptr); return NULL; } /* Previous data must be preserved, if possible */ memcpy(new_ptr, ptr, MIN(B_DATA_SIZE(b), B_DATA_SIZE(B_HEAD(new_ptr)))); shee(ptr); return new_ptr; }
int main(int argc, char *argv[]) { char *str; if (argc > 1) { str = shalloc(strlen(argv[1]) + 1); sprintf(str, "%s\n", argv[1]); } else { str = shalloc(sizeof(HELLO)); sprintf(str, HELLO); } shapass(str); return 0; }
void *malloc(size_t size) { LOG("someone called malloc()\n"); return shalloc(size); }