/* * First time an entry is used, its base and size are set. * An entry can be freed and re-malloc'd but its base & size don't change. * Should be smart enough for needs of bootwrapper.
*/ staticvoid *simple_malloc(unsignedlong size)
{ unsignedlong i; struct alloc_info *p = alloc_tbl;
if (size == 0) goto err_out;
size = _ALIGN_UP(size, alloc_min);
for (i=0; i<tbl_entries; i++, p++) if (!(p->flags & ENTRY_BEEN_USED)) { /* never been used */ if (size <= space_left) {
p->base = next_base;
p->size = size;
p->flags = ENTRY_BEEN_USED | ENTRY_IN_USE;
next_base += size;
space_left -= size; return (void *)p->base;
} goto err_out; /* not enough space left */
} /* reuse an entry keeping same base & size */ elseif (!(p->flags & ENTRY_IN_USE) && (size <= p->size)) {
p->flags |= ENTRY_IN_USE; return (void *)p->base;
}
err_out: return NULL;
}
/* * Change size of area pointed to by 'ptr' to 'size'. * If 'ptr' is NULL, then its a malloc(). If 'size' is 0, then its a free(). * 'ptr' must be NULL or a pointer to a non-freed area previously returned by * simple_realloc() or simple_malloc().
*/ staticvoid *simple_realloc(void *ptr, unsignedlong size)
{ struct alloc_info *p; void *new;
if (size == 0) {
simple_free(ptr); return NULL;
}
if (ptr == NULL) return simple_malloc(size);
p = simple_find_entry(ptr); if (p == NULL) /* ptr not from simple_malloc/simple_realloc */ return NULL; if (size <= p->size) /* fits in current block */ return ptr;
new = simple_malloc(size); if (new) {
memcpy(new, ptr, p->size);
simple_free(ptr);
}
returnnew;
}
/* * Returns addr of first byte after heap so caller can see if it took * too much space. If so, change args & try again.
*/ void *simple_alloc_init(char *base, unsignedlong heap_size, unsignedlong granularity, unsignedlong max_allocs)
{ unsignedlong heap_base, tbl_size;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.