void f_heap_op_reverseorder() { int i; char** addresses = (char**)malloc(sizeof(char*) * g_op_count); /* allocate test data in order */ for (i = 0; i < g_op_count; i ++) { addresses[i] = (char*)alloc(g_alloc, g_heap, get_alloc_size(i)); } if (g_fill_data) { for (i = 0; i < g_op_count; i ++) { int size = get_alloc_size(i); int j; for (j = 0; j < size; j ++) { ((char*)addresses[i])[j] = 0; } } } /* last, dealloc data in reverse order */ for (i = g_op_count-1; i >= 0; i --) { dealloc(g_dealloc, g_heap, addresses[i]); } free(addresses); }
char *ft_itoa(int n) { char *nbr; long long_nbr; long_nbr = (long)n; nbr = (char*)malloc(sizeof(char) * (get_alloc_size(n) + 1)); if (nbr == NULL) return (NULL); if (long_nbr < 0) { nbr[0] = '-'; long_nbr = -long_nbr; } rec_fill_number(nbr, long_nbr, get_alloc_size(n) - 1); return (nbr); }
static char has_one_page_available() { char *mem; char av; av = 0; mem = get_page_list(0, (void *)-1); while (mem && av != 3) { if (*PAGE_SIZE(mem) == get_alloc_size(TINY)) av |= 1; else if (*PAGE_SIZE(mem) == get_alloc_size(SMALL)) av |= 2; mem = *PAGE_NEXT(mem); } return (av == 3); }
void file_write(File *in, const unsigned char *offset, size_t read) { unsigned char *data = in->data; File out = (File){.name = in->name, .len = data + (in->len - 1) - offset}; size_t total_read = data + read - offset; asprintf(&out.fullname, "%s/%s", in->output, out.name); out.fp = fopen(out.fullname, "wb"); if (!out.fp) { fprintf(stderr, "file_open(%s) failed: %d\n", out.fullname, errno); perror("Error"); free(out.fullname); return; } fwrite(offset, 1, total_read, out.fp); do { read = file_remap(in, total_read); fwrite(in->data, 1, read, out.fp); total_read += read; } while((total_read < out.len)); fclose(out.fp); free(out.fullname); } size_t file_remap(File *in, size_t total) { if (!in->data) { in->data = malloc(get_alloc_size(in->len)); } size_t read = (total + BYTECHUNK < in->len) ? BYTECHUNK : (in->len - total); fread(in->data, 1, read, in->fp); return read; }
void* realloc(void* ptr, int s) { void* new_ptr = NULL ; if(s) new_ptr = (void*)malloc(s) ; if(ptr) { int old_size ; if(get_alloc_size(ptr, &old_size) < 0) return (void*)NULL ; int copy_size = (old_size < s) ? old_size : s ; memcpy(new_ptr, ptr, copy_size) ; free(ptr) ; } return new_ptr ; }
void f_heap_op_random() { int i; int freelist_head = 0; int alloclist_head = -1; int op = 1; /* 1 means alloc, 0 means dealloc */ int pool_size = 5 * g_heap_op_alter_range_max; /* the algorithm is a little complicated, but it ensures performance */ char** address = (char**)malloc(sizeof(char*) * pool_size); int* freelist = (int*)malloc(sizeof(int) * pool_size); int* alloclist = (int*)malloc(sizeof(int) * pool_size); /* form a memory pool to fast find an allocable unit */ for (i = 0; i < pool_size; i ++) { address[i] = NULL; freelist[i] = i + 1; alloclist[i] = -1; } freelist[pool_size-1] = -1; for (i = 0; i < g_op_count; i ++) { op = rand() % 2; if (op == 1) { /* op == 1, we should alloc */ if (freelist_head == -1) { /* no allocable address, just do nothing */ } else { dbg_assert(address[freelist_head] == NULL); address[freelist_head] = (char*)alloc(g_alloc, g_heap, get_alloc_size(i)); /* add into allocated list */ alloclist[freelist_head] = alloclist_head; alloclist_head = freelist_head; /* remove from freelist */ freelist_head = freelist[freelist_head]; } } else { /* op == -1, we should dealloc, dealloc the first address in the alloc list */ if (alloclist_head == -1) { /* all are freed, do nothing */ } else { dbg_assert(address[alloclist_head] != NULL); dealloc(g_dealloc, g_heap, address[alloclist_head]); address[alloclist_head] = NULL; /* add into free list */ freelist[alloclist_head] = freelist_head; freelist_head = alloclist_head; /* remove from alloclist */ alloclist_head = alloclist[alloclist_head]; } } } /* dealloc all allocated if they are not deleted */ for (i = 0; i < pool_size; i ++) { if (address[i] != NULL) { dealloc(g_dealloc, g_heap, address[i]); address[i] = NULL; } } free(address); free(freelist); free(alloclist); }
void f_heap_op_alternately() { int i; int step_length = -1, step = 0; int freelist_head = 0; int alloclist_head = -1; int op = 1; /* 1 means alloc, -1 means dealloc */ int pool_size = 5 * g_heap_op_alter_range_max; /* the algorithm is a little complicated, but it ensures performance */ char** address = (char**)malloc(sizeof(char*) * pool_size); int* freelist = (int*)malloc(sizeof(int) * pool_size); int* alloclist = (int*)malloc(sizeof(int) * pool_size); /* form a memory pool to fast find an allocable unit */ for (i = 0; i < pool_size; i ++) { address[i] = NULL; freelist[i] = i + 1; alloclist[i] = -1; } freelist[pool_size-1] = -1; step_length = generate_in_range(g_heap_op_alter_range_min, g_heap_op_alter_range_max); for (i = 0; i < g_op_count; i ++) { if (op == 1) { /* op == 1, we should alloc */ if (freelist_head == -1) { /* no allocable address, just do nothing */ } else { dbg_assert(address[freelist_head] == NULL); address[freelist_head] = (char*)alloc(g_alloc, g_heap, get_alloc_size(i)); /* add into allocated list */ alloclist[freelist_head] = alloclist_head; alloclist_head = freelist_head; /* remove from freelist */ freelist_head = freelist[freelist_head]; } } else { dbg_assert(op == -1); /* op == -1, we should dealloc, dealloc the first address in the alloc list */ if (alloclist_head == -1) { /* all are freed, do nothing */ } else { dbg_assert(address[alloclist_head] != NULL); dealloc(g_dealloc, g_heap, address[alloclist_head]); address[alloclist_head] = NULL; /* add into free list */ freelist[alloclist_head] = freelist_head; freelist_head = alloclist_head; /* remove from alloclist */ alloclist_head = alloclist[alloclist_head]; } } step ++; if (step >= step_length) { step_length = generate_in_range(g_heap_op_alter_range_min, g_heap_op_alter_range_max); step = 0; /* we have done enough times of one kind operation, invert */ op = -op; } } /* dealloc all allocated if they are not deleted */ for (i = 0; i < pool_size; i ++) { if (address[i] != NULL) { dealloc(g_dealloc, g_heap, address[i]); address[i] = NULL; } } free(address); free(freelist); free(alloclist); }