Commit fb01d1fb authored by Wayne Davison's avatar Wayne Davison

Changed the POOL_QALIGN flag to POOL_NO_QALIGN, reversing the setting

(making pools aligned by default).  Added the missing code to make the
documented behavior of pool_free() with a NULL addr work.  Updated the
pool_alloc.3 manpage.
parent 51ce67d5
...@@ -2432,7 +2432,7 @@ struct file_list *flist_new(int flags, char *msg) ...@@ -2432,7 +2432,7 @@ struct file_list *flist_new(int flags, char *msg)
if (flags & FLIST_TEMP) { if (flags & FLIST_TEMP) {
if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0, if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
out_of_memory, out_of_memory,
POOL_INTERN|POOL_QALIGN))) POOL_INTERN)))
out_of_memory(msg); out_of_memory(msg);
} else { } else {
/* This is a doubly linked list with prev looping back to /* This is a doubly linked list with prev looping back to
...@@ -2440,7 +2440,7 @@ struct file_list *flist_new(int flags, char *msg) ...@@ -2440,7 +2440,7 @@ struct file_list *flist_new(int flags, char *msg)
if (!first_flist) { if (!first_flist) {
flist->file_pool = pool_create(NORMAL_EXTENT, 0, flist->file_pool = pool_create(NORMAL_EXTENT, 0,
out_of_memory, out_of_memory,
POOL_INTERN|POOL_QALIGN); POOL_INTERN);
if (!flist->file_pool) if (!flist->file_pool)
out_of_memory(msg); out_of_memory(msg);
......
...@@ -95,25 +95,39 @@ for ...@@ -95,25 +95,39 @@ for
.I quantum .I quantum
will produce a quantum that should meet maximal alignment will produce a quantum that should meet maximal alignment
on most platforms. on most platforms.
If Unless
.B POOL_QALIGN .B POOL_NO_QALIGN
is set in the is set in the
.IR flags , .IR flags ,
allocations will be aligned to addresses that are a allocations will be aligned to addresses that are a
multiple of multiple of
.IR quantum . .IR quantum .
A
.B NULL
may be specified for the
.I bomb
function pointer if it is not needed. (See the
.B pool_alloc()
function for how it is used.)
If If
.B POOL_CLEAR .B POOL_CLEAR
is set in the is set in the
.IR flags , .IR flags ,
all allocations from the pool will be initialized to zeros. all allocations from the pool will be initialized to zeros.
You may specify a If either
.B NULL .B POOL_PREPEND
for the or
.I bomb .B POOL_INTERN
function pointer if you don't wish to use it. (See the is specified in the
.B pool_alloc() .IR flags ,
function for how it is used.) each extent's data structure will be allocated at the start of the
.IR size -length
buffer (rather than as a separate, non-pool allocation), with the
former extending the
.I size
to hold the structure, and the latter subtracting the structure's
length from the indicated
.IR size .
.P .P
.B pool_destroy() .B pool_destroy()
destroys an allocation destroys an allocation
...@@ -131,8 +145,8 @@ is ...@@ -131,8 +145,8 @@ is
.BR 0 , .BR 0 ,
.I quantum .I quantum
bytes will be allocated. bytes will be allocated.
If the pool has been created with If the pool has been created without
.BR POOL_QALIGN , .BR POOL_NO_QALIGN ,
every chunk of memory that is returned will be suitably aligned. every chunk of memory that is returned will be suitably aligned.
You can use this with the default You can use this with the default
.I quantum .I quantum
...@@ -169,7 +183,7 @@ an extent), its memory will be completely freed back to the system. ...@@ -169,7 +183,7 @@ an extent), its memory will be completely freed back to the system.
If If
.I addr .I addr
is is
.BR 0 , .BR NULL ,
no memory will be freed, but subsequent allocations will come no memory will be freed, but subsequent allocations will come
from a new extent. from a new extent.
.P .P
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#define POOL_DEF_EXTENT (32 * 1024) #define POOL_DEF_EXTENT (32 * 1024)
#define POOL_QALIGN_P2 (1<<16) #define POOL_QALIGN_P2 (1<<16) /* power-of-2 qalign */
struct alloc_pool struct alloc_pool
{ {
...@@ -72,8 +72,8 @@ pool_create(size_t size, size_t quantum, void (*bomb)(const char *), int flags) ...@@ -72,8 +72,8 @@ pool_create(size_t size, size_t quantum, void (*bomb)(const char *), int flags)
} }
if (quantum <= 1) if (quantum <= 1)
flags &= ~(POOL_QALIGN | POOL_QALIGN_P2); flags = (flags | POOL_NO_QALIGN) & ~POOL_QALIGN_P2;
else if (flags & POOL_QALIGN) { else if (!(flags & POOL_NO_QALIGN)) {
if (size % quantum) if (size % quantum)
size += quantum - size % quantum; size += quantum - size % quantum;
/* If quantum is a power of 2, we'll avoid using modulus. */ /* If quantum is a power of 2, we'll avoid using modulus. */
...@@ -123,7 +123,7 @@ pool_alloc(alloc_pool_t p, size_t len, const char *bomb_msg) ...@@ -123,7 +123,7 @@ pool_alloc(alloc_pool_t p, size_t len, const char *bomb_msg)
else if (pool->flags & POOL_QALIGN_P2) { else if (pool->flags & POOL_QALIGN_P2) {
if (len & (pool->quantum - 1)) if (len & (pool->quantum - 1))
len += pool->quantum - (len & (pool->quantum - 1)); len += pool->quantum - (len & (pool->quantum - 1));
} else if (pool->flags & POOL_QALIGN) { } else if (!(pool->flags & POOL_NO_QALIGN)) {
if (len % pool->quantum) if (len % pool->quantum)
len += pool->quantum - len % pool->quantum; len += pool->quantum - len % pool->quantum;
} }
...@@ -185,12 +185,21 @@ pool_free(alloc_pool_t p, size_t len, void *addr) ...@@ -185,12 +185,21 @@ pool_free(alloc_pool_t p, size_t len, void *addr)
if (!pool) if (!pool)
return; return;
if (!addr) {
/* A NULL addr starts a fresh extent for new allocations. */
if ((cur = pool->extents) != NULL && cur->free != pool->size) {
cur->bound += cur->free;
cur->free = 0;
}
return;
}
if (!len) if (!len)
len = pool->quantum; len = pool->quantum;
else if (pool->flags & POOL_QALIGN_P2) { else if (pool->flags & POOL_QALIGN_P2) {
if (len & (pool->quantum - 1)) if (len & (pool->quantum - 1))
len += pool->quantum - (len & (pool->quantum - 1)); len += pool->quantum - (len & (pool->quantum - 1));
} else if (pool->flags & POOL_QALIGN) { } else if (!(pool->flags & POOL_NO_QALIGN)) {
if (len % pool->quantum) if (len % pool->quantum)
len += pool->quantum - len % pool->quantum; len += pool->quantum - len % pool->quantum;
} }
......
#include <stddef.h> #include <stddef.h>
#define POOL_CLEAR (1<<0) /* zero fill allocations */ #define POOL_CLEAR (1<<0) /* zero fill allocations */
#define POOL_QALIGN (1<<1) /* align data to quanta */ #define POOL_NO_QALIGN (1<<1) /* don't align data to quanta */
#define POOL_INTERN (1<<2) /* Allocate extent structures */ #define POOL_INTERN (1<<2) /* Allocate extent structures */
#define POOL_PREPEND (1<<3) /* or prepend to extent data */ #define POOL_PREPEND (1<<3) /* or prepend to extent data */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment