Ejemplo n.º 1
0
void initialize_memory(void) {
    SIZET i, j;
    if ((MEMORY_ALIGNMENT & (MEMORY_ALIGNMENT-1)) != 0) {
	fprintf(stderr, "sizeof(Align) is not a power of 2.\n");
	exit(1);	
    }
    for (i=0, j=1; i < j; i = j, j = (2*j+1)) largest_block = i;
    largest_block &= ALIGNMENT_MASK;
    largest_block += -sizeof(Nuggie); /* must have room for a nuggie too */
    
    start_of_array = (void *) malloc(MEMORY_LIMIT);
    if (start_of_array == NULL) {
	fprintf(stderr, "Not enough memory for base pool.\n");
	exit(1);
    }

    end_of_array = start_of_array + ((sizeof(Nuggie) + MEMORY_ALIGNMENT-1) & ALIGNMENT_MASK);
    size_lval(end_of_array) = 0;
    max_space_in_use = 0;
    space_in_use = 0;
    last_block = NULL;
}
void * xalloc(int size) {
    char * old_end_of_array;
    if (((unsigned int) size) > largest_block) {
	fprintf(stderr, "Attempt to allocate too big a block (more than %d bytes)\n", largest_block);
	abort();
	exit(1);
    }
    old_end_of_array = end_of_array;
    end_of_array += ((size+sizeof(Nuggie)) + MEMORY_ALIGNMENT-1) & ALIGNMENT_MASK;
    size_lval(end_of_array) = end_of_array - old_end_of_array;
    clear_use(end_of_array);  /* this is not necessary, cause it will be zero */
    set_use(old_end_of_array);
    last_block = old_end_of_array;

    if (((unsigned int)(end_of_array-start_of_array)) > MEMORY_LIMIT) {
	fprintf(stderr, "Ran out of space.  Memory requested so far: %d bytes\n",
	       (end_of_array - start_of_array));
	exit(1);
    }
    space_in_use += size;
    if (space_in_use > max_space_in_use) max_space_in_use = space_in_use;
    return old_end_of_array;
}