[nginx] Slab: improved code readability.
Ruslan Ermilov
ru at nginx.com
Wed Dec 7 19:27:29 UTC 2016
details: http://hg.nginx.org/nginx/rev/88c8c3d65184
branches:
changeset: 6823:88c8c3d65184
user: Ruslan Ermilov <ru at nginx.com>
date: Wed Dec 07 22:25:37 2016 +0300
description:
Slab: improved code readability.
No functional changes.
diffstat:
src/core/ngx_slab.c | 103 ++++++++++++++++++++++++---------------------------
1 files changed, 49 insertions(+), 54 deletions(-)
diffs (302 lines):
diff -r c045b4926b2c -r 88c8c3d65184 src/core/ngx_slab.c
--- a/src/core/ngx_slab.c Wed Dec 07 19:03:31 2016 +0300
+++ b/src/core/ngx_slab.c Wed Dec 07 22:25:37 2016 +0300
@@ -41,6 +41,19 @@
#endif
+#define ngx_slab_slots(pool) \
+ (ngx_slab_page_t *) ((u_char *) (pool) + sizeof(ngx_slab_pool_t))
+
+#define ngx_slab_page_type(page) ((page)->prev & NGX_SLAB_PAGE_MASK)
+
+#define ngx_slab_page_prev(page) \
+ (ngx_slab_page_t *) ((page)->prev & ~NGX_SLAB_PAGE_MASK)
+
+#define ngx_slab_page_addr(pool, page) \
+ ((((page) - (pool)->pages) << ngx_pagesize_shift) \
+ + (uintptr_t) (pool)->start)
+
+
#if (NGX_DEBUG_MALLOC)
#define ngx_slab_junk(p, size) ngx_memset(p, 0xA5, size)
@@ -76,7 +89,7 @@ ngx_slab_init(ngx_slab_pool_t *pool)
size_t size;
ngx_int_t m;
ngx_uint_t i, n, pages;
- ngx_slab_page_t *slots;
+ ngx_slab_page_t *slots, *page;
/* STUB */
if (ngx_slab_max_size == 0) {
@@ -90,12 +103,13 @@ ngx_slab_init(ngx_slab_pool_t *pool)
pool->min_size = 1 << pool->min_shift;
- p = (u_char *) pool + sizeof(ngx_slab_pool_t);
+ slots = ngx_slab_slots(pool);
+
+ p = (u_char *) slots;
size = pool->end - p;
ngx_slab_junk(p, size);
- slots = (ngx_slab_page_t *) p;
n = ngx_pagesize_shift - pool->min_shift;
for (i = 0; i < n; i++) {
@@ -108,25 +122,25 @@ ngx_slab_init(ngx_slab_pool_t *pool)
pages = (ngx_uint_t) (size / (ngx_pagesize + sizeof(ngx_slab_page_t)));
- ngx_memzero(p, pages * sizeof(ngx_slab_page_t));
+ pool->pages = (ngx_slab_page_t *) p;
+ ngx_memzero(pool->pages, pages * sizeof(ngx_slab_page_t));
- pool->pages = (ngx_slab_page_t *) p;
+ page = pool->pages;
pool->free.prev = 0;
- pool->free.next = (ngx_slab_page_t *) p;
+ pool->free.next = page;
- pool->pages->slab = pages;
- pool->pages->next = &pool->free;
- pool->pages->prev = (uintptr_t) &pool->free;
+ page->slab = pages;
+ page->next = &pool->free;
+ page->prev = (uintptr_t) &pool->free;
- pool->start = (u_char *)
- ngx_align_ptr((uintptr_t) p + pages * sizeof(ngx_slab_page_t),
- ngx_pagesize);
+ pool->start = ngx_align_ptr(p + pages * sizeof(ngx_slab_page_t),
+ ngx_pagesize);
m = pages - (pool->end - pool->start) / ngx_pagesize;
if (m > 0) {
pages -= m;
- pool->pages->slab = pages;
+ page->slab = pages;
}
pool->last = pool->pages + pages;
@@ -168,8 +182,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
page = ngx_slab_alloc_pages(pool, (size >> ngx_pagesize_shift)
+ ((size % ngx_pagesize) ? 1 : 0));
if (page) {
- p = (page - pool->pages) << ngx_pagesize_shift;
- p += (uintptr_t) pool->start;
+ p = ngx_slab_page_addr(pool, page);
} else {
p = 0;
@@ -191,7 +204,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, ngx_cycle->log, 0,
"slab alloc: %uz slot: %ui", size, slot);
- slots = (ngx_slab_page_t *) ((u_char *) pool + sizeof(ngx_slab_pool_t));
+ slots = ngx_slab_slots(pool);
page = slots[slot].next;
if (page->next != page) {
@@ -199,8 +212,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
if (shift < ngx_slab_exact_shift) {
do {
- p = (page - pool->pages) << ngx_pagesize_shift;
- bitmap = (uintptr_t *) (pool->start + p);
+ bitmap = (uintptr_t *) ngx_slab_page_addr(pool, page);
map = (1 << (ngx_pagesize_shift - shift))
/ (sizeof(uintptr_t) * 8);
@@ -228,8 +240,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
}
}
- prev = (ngx_slab_page_t *)
- (page->prev & ~NGX_SLAB_PAGE_MASK);
+ prev = ngx_slab_page_prev(page);
prev->next = page->next;
page->next->prev = page->prev;
@@ -261,8 +272,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
page->slab |= m;
if (page->slab == NGX_SLAB_BUSY) {
- prev = (ngx_slab_page_t *)
- (page->prev & ~NGX_SLAB_PAGE_MASK);
+ prev = ngx_slab_page_prev(page);
prev->next = page->next;
page->next->prev = page->prev;
@@ -270,9 +280,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
page->prev = NGX_SLAB_EXACT;
}
- p = (page - pool->pages) << ngx_pagesize_shift;
- p += i << shift;
- p += (uintptr_t) pool->start;
+ p = ngx_slab_page_addr(pool, page) + (i << shift);
goto done;
}
@@ -303,8 +311,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
page->slab |= m;
if ((page->slab & NGX_SLAB_MAP_MASK) == mask) {
- prev = (ngx_slab_page_t *)
- (page->prev & ~NGX_SLAB_PAGE_MASK);
+ prev = ngx_slab_page_prev(page);
prev->next = page->next;
page->next->prev = page->prev;
@@ -312,9 +319,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
page->prev = NGX_SLAB_BIG;
}
- p = (page - pool->pages) << ngx_pagesize_shift;
- p += i << shift;
- p += (uintptr_t) pool->start;
+ p = ngx_slab_page_addr(pool, page) + (i << shift);
goto done;
}
@@ -330,8 +335,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
if (page) {
if (shift < ngx_slab_exact_shift) {
- p = (page - pool->pages) << ngx_pagesize_shift;
- bitmap = (uintptr_t *) (pool->start + p);
+ bitmap = (uintptr_t *) ngx_slab_page_addr(pool, page);
s = 1 << shift;
n = (1 << (ngx_pagesize_shift - shift)) / 8 / s;
@@ -354,8 +358,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
slots[slot].next = page;
- p = ((page - pool->pages) << ngx_pagesize_shift) + s * n;
- p += (uintptr_t) pool->start;
+ p = ngx_slab_page_addr(pool, page) + s * n;
goto done;
@@ -367,8 +370,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
slots[slot].next = page;
- p = (page - pool->pages) << ngx_pagesize_shift;
- p += (uintptr_t) pool->start;
+ p = ngx_slab_page_addr(pool, page);
goto done;
@@ -380,8 +382,7 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *p
slots[slot].next = page;
- p = (page - pool->pages) << ngx_pagesize_shift;
- p += (uintptr_t) pool->start;
+ p = ngx_slab_page_addr(pool, page);
goto done;
}
@@ -456,7 +457,7 @@ ngx_slab_free_locked(ngx_slab_pool_t *po
n = ((u_char *) p - pool->start) >> ngx_pagesize_shift;
page = &pool->pages[n];
slab = page->slab;
- type = page->prev & NGX_SLAB_PAGE_MASK;
+ type = ngx_slab_page_type(page);
switch (type) {
@@ -478,8 +479,7 @@ ngx_slab_free_locked(ngx_slab_pool_t *po
if (bitmap[n] & m) {
if (page->next == NULL) {
- slots = (ngx_slab_page_t *)
- ((u_char *) pool + sizeof(ngx_slab_pool_t));
+ slots = ngx_slab_slots(pool);
slot = shift - pool->min_shift;
page->next = slots[slot].next;
@@ -528,8 +528,7 @@ ngx_slab_free_locked(ngx_slab_pool_t *po
if (slab & m) {
if (slab == NGX_SLAB_BUSY) {
- slots = (ngx_slab_page_t *)
- ((u_char *) pool + sizeof(ngx_slab_pool_t));
+ slots = ngx_slab_slots(pool);
slot = ngx_slab_exact_shift - pool->min_shift;
page->next = slots[slot].next;
@@ -567,8 +566,7 @@ ngx_slab_free_locked(ngx_slab_pool_t *po
if (slab & m) {
if (page->next == NULL) {
- slots = (ngx_slab_page_t *)
- ((u_char *) pool + sizeof(ngx_slab_pool_t));
+ slots = ngx_slab_slots(pool);
slot = shift - pool->min_shift;
page->next = slots[slot].next;
@@ -705,7 +703,6 @@ static void
ngx_slab_free_pages(ngx_slab_pool_t *pool, ngx_slab_page_t *page,
ngx_uint_t pages)
{
- ngx_uint_t type;
ngx_slab_page_t *prev, *join;
page->slab = pages--;
@@ -715,7 +712,7 @@ ngx_slab_free_pages(ngx_slab_pool_t *poo
}
if (page->next) {
- prev = (ngx_slab_page_t *) (page->prev & ~NGX_SLAB_PAGE_MASK);
+ prev = ngx_slab_page_prev(page);
prev->next = page->next;
page->next->prev = page->prev;
}
@@ -723,15 +720,14 @@ ngx_slab_free_pages(ngx_slab_pool_t *poo
join = page + page->slab;
if (join < pool->last) {
- type = join->prev & NGX_SLAB_PAGE_MASK;
- if (type == NGX_SLAB_PAGE) {
+ if (ngx_slab_page_type(join) == NGX_SLAB_PAGE) {
if (join->next != NULL) {
pages += join->slab;
page->slab += join->slab;
- prev = (ngx_slab_page_t *) (join->prev & ~NGX_SLAB_PAGE_MASK);
+ prev = ngx_slab_page_prev(join);
prev->next = join->next;
join->next->prev = join->prev;
@@ -744,19 +740,18 @@ ngx_slab_free_pages(ngx_slab_pool_t *poo
if (page > pool->pages) {
join = page - 1;
- type = join->prev & NGX_SLAB_PAGE_MASK;
- if (type == NGX_SLAB_PAGE) {
+ if (ngx_slab_page_type(join) == NGX_SLAB_PAGE) {
if (join->slab == NGX_SLAB_PAGE_FREE) {
- join = (ngx_slab_page_t *) (join->prev & ~NGX_SLAB_PAGE_MASK);
+ join = ngx_slab_page_prev(join);
}
if (join->next != NULL) {
pages += join->slab;
join->slab += page->slab;
- prev = (ngx_slab_page_t *) (join->prev & ~NGX_SLAB_PAGE_MASK);
+ prev = ngx_slab_page_prev(join);
prev->next = join->next;
join->next->prev = join->prev;
More information about the nginx-devel
mailing list