X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fthreads%2Fmalloc.c;h=72b441488393d862daed5c1a8ba367314089a90a;hb=c848687b0825179f834e4b1b7a1a52579d902744;hp=ba39d1d926f496115a8cf703265808fa3813f3ea;hpb=3fc16f6e9abc98a3bd5427eb210669860609a224;p=pintos-anon diff --git a/src/threads/malloc.c b/src/threads/malloc.c index ba39d1d..72b4414 100644 --- a/src/threads/malloc.c +++ b/src/threads/malloc.c @@ -31,7 +31,7 @@ malloc_init (void) { size_t slot_size; - for (slot_size = 16; slot_size < NBPG; slot_size *= 2) + for (slot_size = 16; slot_size < PGSIZE; slot_size *= 2) { struct desc *d = &descs[desc_cnt++]; ASSERT (desc_cnt <= sizeof descs / sizeof *descs); @@ -44,7 +44,7 @@ malloc_init (void) static struct arena * slot_to_arena (struct slot *s) { - return (struct arena *) ((uint32_t) s & ~(NBPG - 1)); + return (struct arena *) ((uint32_t) s & ~(PGSIZE - 1)); } static void * @@ -71,7 +71,7 @@ malloc (size_t size) break; if (d == descs + desc_cnt) { - printk ("can't malloc %zu byte object\n", size); + printk ("malloc: %zu byte allocation too big\n", size); return NULL; } @@ -87,7 +87,7 @@ malloc (size_t size) a->next = d->arenas; if (d->arenas != NULL) d->arenas->prev = a; - for (ofs = sizeof *a; ofs + d->slot_size <= NBPG; ofs += d->slot_size) + for (ofs = sizeof *a; ofs + d->slot_size <= PGSIZE; ofs += d->slot_size) { struct slot *s = (struct slot *) ((uint8_t *) a + ofs); s->next = d->free_list; @@ -97,6 +97,23 @@ malloc (size_t size) return get_free_slot (d); } +void * +calloc (size_t a, size_t b) +{ + void *p; + size_t size; + + size = a * b; + if (size < a || size < b) + return NULL; + + p = malloc (size); + if (p != NULL) + memset (p, 0, size); + + return p; +} + void free (void *p) {