diff options
author | neal <neal> | 2005-01-07 10:27:50 +0000 |
---|---|---|
committer | neal <neal> | 2005-01-07 10:27:50 +0000 |
commit | a34443c2b24f3def14b8f10e34c9cd5d7d7c42d3 (patch) | |
tree | e4aec5f0811c4689b0fa0bec5835fb2fd560ca7f /libhurd-slab | |
parent | ca7c322232745f31ec6980864ca569b74f57e68b (diff) |
libhurd-slab/
2005-01-06 Neal H. Walfield <neal@gnu.org>
* slab.h (hurd_slab_allocate_buffer_t): New type.
(hurd_slab_deallocate_buffer_t): Likewise.
(struct hurd_slab_space): New fields allocate_buffer and
deallocate_buffer.
(HURD_SLAB_SPACE_INITIALIZER): Add new arguments ALLOC and DEALLOC
and take them into account when creating the slab.
(hurd_slab_create): New parameters ALLOCATE_BUFFER and
DEALLOCATE_BUFFER.
(hurd_slab_init): Likewise.
* slab.c (allocate_buffer): New function.
(deallocate_buffer): Likewise.
(reap): When deallocating a buffer, use allocate_buffer.
(grow): When allocating or deallocating a buffer, call
allocate_buffer or deallocate_buffer as appropriate.
(hurd_slab_init): Add new arguments ALLOCATE_BUFFER and
DEALLOCATE_BUFFER and take them into account when setting up
SPACE.
(hurd_slab_create): Likewise.
libhurd-cap/
2005-01-07 Neal H. Walfield <neal@gnu.org>
* cap.c (hurd_cap_init): Supply the allocate_buffer and
deallocate_buffer arguments to hurd_slab_create to conform with
the new semantics.
libhurd-cap-server/
2005-01-07 Neal H. Walfield <neal@gnu.org>
* class-init.c (hurd_cap_class_init_untyped): Supply the
allocate_buffer and deallocate_buffer arguments to
hurd_slab_create to conform with the new semantics.
* obj-entry-space.c (_hurd_cap_obj_entry_space): Likewise for
HURD_SLAB_SPACE_INITIALIZER.
* client-create.c (_hurd_cap_client_space): Likewise.
task/
2005-01-07 Neal H. Walfield <neal@gnu.org>
* thread.c (threads): Supply the allocate_buffer and
deallocate_buffer arguments to HURD_SLAB_SPACE_INITIALIZER to
conform with the new semantics.
Diffstat (limited to 'libhurd-slab')
-rw-r--r-- | libhurd-slab/ChangeLog | 22 | ||||
-rw-r--r-- | libhurd-slab/README | 4 | ||||
-rw-r--r-- | libhurd-slab/slab.c | 63 | ||||
-rw-r--r-- | libhurd-slab/slab.h | 46 |
4 files changed, 116 insertions, 19 deletions
diff --git a/libhurd-slab/ChangeLog b/libhurd-slab/ChangeLog index b1acb7e..378834f 100644 --- a/libhurd-slab/ChangeLog +++ b/libhurd-slab/ChangeLog @@ -1,3 +1,25 @@ +2005-01-06 Neal H. Walfield <neal@gnu.org> + + * slab.h (hurd_slab_allocate_buffer_t): New type. + (hurd_slab_deallocate_buffer_t): Likewise. + (struct hurd_slab_space): New fields allocate_buffer and + deallocate_buffer. + (HURD_SLAB_SPACE_INITIALIZER): Add new arguments ALLOC and DEALLOC + and take them into account when creating the slab. + (hurd_slab_create): New parameters ALLOCATE_BUFFER and + DEALLOCATE_BUFFER. + (hurd_slab_init): Likewise. + + * slab.c (allocate_buffer): New function. + (deallocate_buffer): Likewise. + (reap): When deallocating a buffer, use allocate_buffer. + (grow): When allocating or deallocating a buffer, call + allocate_buffer or deallocate_buffer as appropriate. + (hurd_slab_init): Add new arguments ALLOCATE_BUFFER and + DEALLOCATE_BUFFER and take them into account when setting up + SPACE. + (hurd_slab_create): Likewise. + 2004-11-01 Marcus Brinkmann <marcus@gnu.org> * slab.c (hurd_slab_destroy): Remove unused variable PREVP. diff --git a/libhurd-slab/README b/libhurd-slab/README index 4d6e6d4..d171a73 100644 --- a/libhurd-slab/README +++ b/libhurd-slab/README @@ -7,8 +7,8 @@ purpose allocation strategy like malloc(), but a special arena (or other backing memory) and a special purpose allocator that can take advantage of the fact that all objects are of the same size. -In addition, such objects usually have an initial stage that is -naturally returned to when the object is not needed anymore. For +In addition, such objects usually have an initial stage that they +naturally return to when the objects are no longer needed. For example locks are unlocked, reference counters are zero, etc. In such a situation, performance can be further increased by caching the unused objects, instead of destroying them. diff --git a/libhurd-slab/slab.c b/libhurd-slab/slab.c index c0a717c..b6e3f51 100644 --- a/libhurd-slab/slab.c +++ b/libhurd-slab/slab.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2003 Free Software Foundation, Inc. +/* Copyright (C) 2003, 2005 Free Software Foundation, Inc. Written by Johan Rydberg. This file is part of the GNU Hurd. @@ -65,6 +65,40 @@ struct hurd_slab union hurd_bufctl *free_list; }; +/* Allocate a buffer in *PTR of size SIZE which must be a power of 2 + and self aligned (i.e. aligned on a SIZE byte boundary) for slab + space SPACE. Return 0 on success, an error code on failure. */ +static error_t +allocate_buffer (struct hurd_slab_space *space, size_t size, void **ptr) +{ + if (space->allocate_buffer) + return space->allocate_buffer (space->hook, getpagesize (), ptr); + else + { + *ptr = mmap (NULL, getpagesize (), PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); + if (*ptr == MAP_FAILED) + return errno; + else + return 0; + } +} + +/* Deallocate buffer BUFFER of size SIZE which was allocated for slab + space SPACE. Return 0 on success, an error code on failure. */ +static error_t +deallocate_buffer (struct hurd_slab_space *space, void *buffer, size_t size) +{ + if (space->deallocate_buffer) + return space->deallocate_buffer (space->hook, buffer, size); + else + { + if (munmap (buffer, size) == -1) + return errno; + else + return 0; + } +} /* Insert SLAB into the list of slabs in SPACE. SLAB is expected to be complete (so it will be inserted at the end). */ @@ -143,9 +177,9 @@ reap (struct hurd_slab_space *space) in front of it), get address by masking with page size. This frees the slab and all its buffers, since they live on the same page. */ - err = munmap - ((void *) (((uintptr_t) s) & ~(getpagesize () - 1)), - getpagesize ()); + err = deallocate_buffer (space, (void *) (((uintptr_t) s) + & ~(getpagesize () - 1)), + getpagesize ()); if (err) break; __hurd_slab_nr_pages--; @@ -200,6 +234,7 @@ init_space (hurd_slab_space_t space) static error_t grow (struct hurd_slab_space *space) { + error_t err; struct hurd_slab *new_slab; union hurd_bufctl *bufctl; int nr_objs, i; @@ -211,10 +246,10 @@ grow (struct hurd_slab_space *space) if (!space->initialized) init_space (space); - p = mmap (NULL, getpagesize (), PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); - if (p == MAP_FAILED) - return errno; + err = allocate_buffer (space, getpagesize (), &p); + if (err) + return err; + __hurd_slab_nr_pages++; new_slab = (p + getpagesize () - sizeof (struct hurd_slab)); @@ -244,7 +279,7 @@ grow (struct hurd_slab_space *space) (*space->destructor) (space->hook, buffer); } - munmap (p, getpagesize ()); + deallocate_buffer (space, p, getpagesize ()); return err; } } @@ -270,6 +305,8 @@ grow (struct hurd_slab_space *space) /* Initialize the slab space SPACE. */ error_t hurd_slab_init (hurd_slab_space_t space, size_t size, size_t alignment, + hurd_slab_allocate_buffer_t allocate_buffer, + hurd_slab_deallocate_buffer_t deallocate_buffer, hurd_slab_constructor_t constructor, hurd_slab_destructor_t destructor, void *hook) @@ -299,6 +336,8 @@ hurd_slab_init (hurd_slab_space_t space, size_t size, size_t alignment, if (err) return err; + space->allocate_buffer = allocate_buffer; + space->deallocate_buffer = deallocate_buffer; space->constructor = constructor; space->destructor = destructor; space->hook = hook; @@ -312,6 +351,8 @@ hurd_slab_init (hurd_slab_space_t space, size_t size, size_t alignment, constructor and destructor. ALIGNMENT can be zero. */ error_t hurd_slab_create (size_t size, size_t alignment, + hurd_slab_allocate_buffer_t allocate_buffer, + hurd_slab_deallocate_buffer_t deallocate_buffer, hurd_slab_constructor_t constructor, hurd_slab_destructor_t destructor, void *hook, @@ -324,7 +365,9 @@ hurd_slab_create (size_t size, size_t alignment, if (!space) return ENOMEM; - err = hurd_slab_init (space, size, alignment, constructor, destructor, hook); + err = hurd_slab_init (space, size, alignment, + allocate_buffer, deallocate_buffer, + constructor, destructor, hook); if (err) { free (space); diff --git a/libhurd-slab/slab.h b/libhurd-slab/slab.h index 5154943..3fa854c 100644 --- a/libhurd-slab/slab.h +++ b/libhurd-slab/slab.h @@ -1,5 +1,5 @@ /* slab.h - The GNU Hurd slab allocator interface. - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2005 Free Software Foundation, Inc. Written by Marcus Brinkmann <marcus@gnu.org> This file is part of the GNU Hurd. @@ -27,6 +27,18 @@ #include <pthread.h> +/* Allocate a buffer in *PTR of size SIZE which must be a power of 2 + and self aligned (i.e. aligned on a SIZE byte boundary). HOOK is + as provided to hurd_slab_create. Return 0 on success, an error + code on failure. */ +typedef error_t (*hurd_slab_allocate_buffer_t) (void *hook, size_t size, + void **ptr); + +/* Deallocate buffer BUFFER of size SIZE. HOOK is as provided to + hurd_slab_create. */ +typedef error_t (*hurd_slab_deallocate_buffer_t) (void *hook, void *buffer, + size_t size); + /* Initialize the slab object pointed to by BUFFER. HOOK is as provided to hurd_slab_create. */ typedef error_t (*hurd_slab_constructor_t) (void *hook, void *buffer); @@ -63,6 +75,12 @@ struct hurd_slab_space size_t requested_size; size_t requested_align; + /* The buffer allocator. */ + hurd_slab_allocate_buffer_t allocate_buffer; + + /* The buffer deallocator. */ + hurd_slab_deallocate_buffer_t deallocate_buffer; + /* The constructor. */ hurd_slab_constructor_t constructor; @@ -94,15 +112,21 @@ struct hurd_slab_space /* Static initializer. TYPE is used to get size and alignment of - objects the slab space will be used to allocate. CTOR and DTOR is - constructor and destructor, respectivly. HOOK is passed as user - data to the constructor and destructor. */ -#define HURD_SLAB_SPACE_INITIALIZER(TYPE, CTOR, DTOR, HOOK) \ + objects the slab space will be used to allocate. ALLOCATE_BUFFER + may be NULL in which case mmap is called. DEALLOCATE_BUFFER may be + NULL in which case munmap is called. CTOR and DTOR are the slab's + object constructor and destructor, respectivly and may be NULL if + not required. HOOK is passed as user data to the constructor and + destructor. */ +#define HURD_SLAB_SPACE_INITIALIZER(TYPE, ALLOC, DEALLOC, CTOR, \ + DTOR, HOOK) \ { \ false, \ PTHREAD_MUTEX_INITIALIZER, \ sizeof (TYPE), \ __alignof__ (TYPE), \ + ALLOC, \ + DEALLOC, \ CTOR, \ DTOR, \ HOOK \ @@ -112,9 +136,15 @@ struct hurd_slab_space /* Create a new slab space with the given object size, alignment, - constructor and destructor. ALIGNMENT can be zero. HOOK is passed - as the first argument to the constructor and destructor. */ + constructor and destructor. ALIGNMENT can be zero. + ALLOCATE_BUFFER may be NULL in which case mmap is called. + DEALLOCATE_BUFFER may be NULL in which case munmap is called. CTOR + and DTOR are the slabs object constructor and destructor, + respectivly and may be NULL if not required. HOOK is passed as the + first argument to the constructor and destructor. */ error_t hurd_slab_create (size_t size, size_t alignment, + hurd_slab_allocate_buffer_t allocate_buffer, + hurd_slab_deallocate_buffer_t deallocate_buffer, hurd_slab_constructor_t constructor, hurd_slab_destructor_t destructor, void *hook, @@ -122,6 +152,8 @@ error_t hurd_slab_create (size_t size, size_t alignment, /* Like hurd_slab_create, but does not allocate storage for the slab. */ error_t hurd_slab_init (hurd_slab_space_t space, size_t size, size_t alignment, + hurd_slab_allocate_buffer_t allocate_buffer, + hurd_slab_deallocate_buffer_t deallocate_buffer, hurd_slab_constructor_t constructor, hurd_slab_destructor_t destructor, void *hook); |