将Nginx源码数组结构(ngx_array.c)和内存池代码单独编译运行,附代码

news2024/11/19 10:47:12

在上面一篇的基础上把Nginx源码数组结构也摘录下来,也增加了测试代码,编译运行。
https://blog.csdn.net/katerdaisy/article/details/132358883
《将nginx内存池代码单独编译运行,了解nginx内存池工作原理,附代码》

在这里插入图片描述

核心代码:

//在内存数组中分三次写入 "hello", "world", "!",然后使用指针加偏移,地址加数据长度两种方式将保存在数组中的文字打印出来
// char mychar[6] = "hello";
// ngx_str_t mydata = {sizeof(mychar),mychar};
ngx_array_t *a = NULL;
a = ngx_array_create(pool, 1, sizeof(ngx_str_t));

ngx_str_t *s = ngx_array_push(a);
s->data = "hello";
s->len = sizeof(s->data);
printf("a->nelts: %ld, a->nalloc:%ld\n",a->nelts,a->nalloc);
printf("ngx_array_push:%s\n",((ngx_str_t *)(a->elts))->data);
printf("ngx_array_push:%s\n",s->data);

ngx_str_t *s1 = ngx_array_push(a);
s1->data = "wrold";
s1->len = sizeof(s1->data);
printf("a->nelts: %ld, a->nalloc:%ld\n",a->nelts,a->nalloc);
printf("ngx_array_push:%s\n",((ngx_str_t *)(a->elts+sizeof(ngx_str_t)))->data);//地址加数据长度
printf("ngx_array_push:%s\n",((ngx_str_t *)(a->elts)+1)->data); //使用指针加偏移
printf("ngx_array_push:%s\n",s1->data);

ngx_str_t *s2 = ngx_array_push(a);
s2->data = "!";
s2->len = sizeof(s2->data);
printf("a->nelts: %ld, a->nalloc:%ld\n",a->nelts,a->nalloc);
printf("ngx_array_push:%s\n",((ngx_str_t *)(a->elts+sizeof(ngx_str_t)*(a->nelts-1)))->data);//地址加数据长度
printf("ngx_array_push:%s\n",((ngx_str_t *)(a->elts)+a->nelts-1)->data); //使用指针加偏移
printf("ngx_array_push:%s\n",s2->data);

结果:

在这里插入图片描述

完整代码:

//-------------------------------------------------------------------------------------------
//下面是一些头文件和宏定义,从nginx的源码中摘录过来
#include </usr/include/stdint.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdarg.h>
#include <stddef.h> /* offsetof() /
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <glob.h>
#include <sys/vfs.h> /
statfs() */

#include <sys/uio.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sched.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h> /* TCP_NODELAY, TCP_CORK */
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/un.h>

#include <time.h> /* tzset() /
#include <malloc.h> /
memalign() /
#include <limits.h> /
IOV_MAX /
#include <sys/ioctl.h>
#include <crypt.h>
#include <sys/utsname.h> /
uname() */

#include <dlfcn.h>

#ifndef ngx_inline
#define ngx_inline inline
#endif

typedef int ngx_fd_t;
typedef intptr_t ngx_int_t;
typedef uintptr_t ngx_uint_t;
typedef intptr_t ngx_flag_t;
typedef struct ngx_module_s ngx_module_t;
typedef struct ngx_conf_s ngx_conf_t;
typedef struct ngx_cycle_s ngx_cycle_t;
typedef struct ngx_pool_s ngx_pool_t;
typedef struct ngx_chain_s ngx_chain_t;
typedef struct ngx_log_s ngx_log_t;
typedef struct ngx_open_file_s ngx_open_file_t;
typedef struct ngx_command_s ngx_command_t;
typedef struct ngx_file_s ngx_file_t;
typedef struct ngx_event_s ngx_event_t;
typedef struct ngx_event_aio_s ngx_event_aio_t;
typedef struct ngx_connection_s ngx_connection_t;

ngx_uint_t ngx_pagesize;
ngx_uint_t ngx_pagesize_shift;
ngx_uint_t ngx_cacheline_size;

// #define NGX_LOG_STDERR 0
// #define NGX_LOG_EMERG 1
// #define NGX_LOG_ALERT 2
// #define NGX_LOG_CRIT 3
// #define NGX_LOG_ERR 4
// #define NGX_LOG_WARN 5
// #define NGX_LOG_NOTICE 6
// #define NGX_LOG_INFO 7
// #define NGX_LOG_DEBUG 8

// #define NGX_LOG_DEBUG_CORE 0x010
// #define NGX_LOG_DEBUG_ALLOC 0x020
// #define NGX_LOG_DEBUG_MUTEX 0x040
// #define NGX_LOG_DEBUG_EVENT 0x080
// #define NGX_LOG_DEBUG_HTTP 0x100
// #define NGX_LOG_DEBUG_MAIL 0x200
// #define NGX_LOG_DEBUG_STREAM 0x400

/*

  • do not forget to update debug_levels[] in src/core/ngx_log.c
  • after the adding a new debug level
    */

#define NGX_LOG_DEBUG_FIRST NGX_LOG_DEBUG_CORE
#define NGX_LOG_DEBUG_LAST NGX_LOG_DEBUG_STREAM
#define NGX_LOG_DEBUG_CONNECTION 0x80000000
#define NGX_LOG_DEBUG_ALL 0x7ffffff0

#ifndef NGX_PALLOC_H_INCLUDED
#define NGX_PALLOC_H_INCLUDED

#ifndef NGX_ALIGNMENT
#define NGX_ALIGNMENT sizeof(unsigned long) /* platform word */
#endif

#define NGX_OK 0
#define NGX_ERROR -1
#define NGX_AGAIN -2
#define NGX_BUSY -3
#define NGX_DONE -4
#define NGX_DECLINED -5
#define NGX_ABORT -6
#define NGX_INVALID_FILE -1
#define NGX_FILE_ERROR -1

#define ngx_errno errno
#define ngx_close_file close
#define ngx_close_file_n “close()”
typedef int ngx_err_t;
#define NGX_ENOENT ENOENT
#define ngx_delete_file_n “unlink()”

/*

  • NGX_MAX_ALLOC_FROM_POOL should be (ngx_pagesize - 1), i.e. 4095 on x86.
  • On Windows NT it decreases a number of locked pages in a kernel.
    */
    #define NGX_MAX_ALLOC_FROM_POOL (ngx_pagesize - 1)

#define NGX_DEFAULT_POOL_SIZE (16 * 1024)

#define NGX_POOL_ALIGNMENT 16
#define NGX_MIN_POOL_SIZE
ngx_align((sizeof(ngx_pool_t) + 2 * sizeof(ngx_pool_large_t)),
NGX_POOL_ALIGNMENT)

typedef void (*ngx_pool_cleanup_pt)(void *data);

typedef struct ngx_pool_cleanup_s ngx_pool_cleanup_t;

struct ngx_pool_cleanup_s {
ngx_pool_cleanup_pt handler;
void *data;
ngx_pool_cleanup_t *next;
};

typedef struct ngx_pool_large_s ngx_pool_large_t;

struct ngx_pool_large_s {
ngx_pool_large_t *next;
void *alloc;
};

typedef struct {
u_char *last;
u_char *end;
ngx_pool_t *next;
ngx_uint_t failed;
} ngx_pool_data_t;

struct ngx_pool_s {
ngx_pool_data_t d;
size_t max;
ngx_pool_t *current;
ngx_chain_t *chain;
ngx_pool_large_t *large;
ngx_pool_cleanup_t *cleanup;
ngx_log_t *log;
};

typedef struct {
ngx_fd_t fd;
u_char *name;
ngx_log_t *log;
} ngx_pool_cleanup_file_t;

void *ngx_alloc(size_t size, ngx_log_t *log);
void *ngx_calloc(size_t size, ngx_log_t *log);

ngx_pool_t *ngx_create_pool(size_t size, ngx_log_t *log);
void ngx_destroy_pool(ngx_pool_t *pool);
void ngx_reset_pool(ngx_pool_t *pool);

void *ngx_palloc(ngx_pool_t *pool, size_t size);
void *ngx_pnalloc(ngx_pool_t *pool, size_t size);
void *ngx_pcalloc(ngx_pool_t *pool, size_t size);
void *ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment);
ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p);

ngx_pool_cleanup_t *ngx_pool_cleanup_add(ngx_pool_t *p, size_t size);
void ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd);
void ngx_pool_cleanup_file(void *data);
void ngx_pool_delete_file(void *data);

#endif /* NGX_PALLOC_H_INCLUDED */

ngx_uint_t ngx_pagesize;
ngx_uint_t ngx_pagesize_shift;
ngx_uint_t ngx_cacheline_size;
#define ngx_free free
#define ngx_memzero(buf, n) (void) memset(buf, 0, n)
#define ngx_memset(buf, c, n) (void) memset(buf, c, n)
#define ngx_align_ptr(p, a)
(u_char *) (((uintptr_t) § + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))
#define ngx_delete_file(name) unlink((const char *) name)
#define ngx_delete_file_n “unlink()”

void *
ngx_alloc(size_t size, ngx_log_t *log)
{
void *p;

p = malloc(size);
if (p == NULL) {
    printf("malloc(%lu) failed\n", size);
}

printf("malloc: %p:%lu\n", p, size);

return p;

}

void *
ngx_calloc(size_t size, ngx_log_t *log)
{
void *p;

p = ngx_alloc(size, log);

if (p) {
    ngx_memzero(p, size);
}

return p;

}

//#define NGX_HAVE_POSIX_MEMALIGN 1
#define NGX_HAVE_MEMALIGN 1
#if (NGX_HAVE_POSIX_MEMALIGN)

void *
ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
{
void *p;
int err;

err = posix_memalign(&p, alignment, size);

if (err) {
    printf("posix_memalign(%lu, %lu) failed\n", alignment, size);
    p = NULL;
}

printf("posix_memalign: %p:%lu @%lu\n", p, size, alignment);

return p;

}

#elif (NGX_HAVE_MEMALIGN)

void *
ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
{
void *p;

p = memalign(alignment, size);
if (p == NULL) {
    printf("memalign(%lu, %lu) failed\n", alignment, size);
}

printf("memalign: %p:%lu @%lu\n", p, size, alignment);

return p;

}

#endif

//-------------------------------------------------------------------------------------------
//下面是从ngx_pcallo.c复制过来的内存池核心代码

static ngx_inline void *ngx_palloc_small(ngx_pool_t *pool, size_t size,
ngx_uint_t align);
static void *ngx_palloc_block(ngx_pool_t *pool, size_t size);
static void *ngx_palloc_large(ngx_pool_t *pool, size_t size);

ngx_pool_t *
ngx_create_pool(size_t size, ngx_log_t *log)
{
ngx_pool_t *p;

p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);
if (p == NULL) {
    return NULL;
}

p->d.last = (u_char *) p + sizeof(ngx_pool_t);
p->d.end = (u_char *) p + size;
p->d.next = NULL;
p->d.failed = 0;

size = size - sizeof(ngx_pool_t);
p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;

p->current = p;
p->chain = NULL;
p->large = NULL;
p->cleanup = NULL;
p->log = log;

return p;

}

void
ngx_destroy_pool(ngx_pool_t *pool)
{
ngx_pool_t *p, *n;
ngx_pool_large_t *l;
ngx_pool_cleanup_t *c;

for (c = pool->cleanup; c; c = c->next) {
    if (c->handler) {
        printf("run cleanup: %p\n", c);
        c->handler(c->data);
    }
}

#if (NGX_DEBUG)

/*
 * we could allocate the pool->log from this pool
 * so we cannot use this log while free()ing the pool
 */

for (l = pool->large; l; l = l->next) {
    printf("free: %p\n", l->alloc);
}

for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
    printf("free: %p, unused: %lu\n", p, p->d.end - p->d.last);

    if (n == NULL) {
        break;
    }
}

#endif

for (l = pool->large; l; l = l->next) {
    if (l->alloc) {
        ngx_free(l->alloc);
    }
}

for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
    ngx_free(p);

    if (n == NULL) {
        break;
    }
}

}

void
ngx_reset_pool(ngx_pool_t *pool)
{
ngx_pool_t *p;
ngx_pool_large_t *l;

for (l = pool->large; l; l = l->next) {
    if (l->alloc) {
        ngx_free(l->alloc);
    }
}

for (p = pool; p; p = p->d.next) {
    p->d.last = (u_char *) p + sizeof(ngx_pool_t);
    p->d.failed = 0;
}

pool->current = pool;
pool->chain = NULL;
pool->large = NULL;

}

void *
ngx_palloc(ngx_pool_t *pool, size_t size)
{
#if !(NGX_DEBUG_PALLOC)
if (size <= pool->max) {
return ngx_palloc_small(pool, size, 1);
}
#endif

return ngx_palloc_large(pool, size);

}

void *
ngx_pnalloc(ngx_pool_t *pool, size_t size)
{
#if !(NGX_DEBUG_PALLOC)
if (size <= pool->max) {
return ngx_palloc_small(pool, size, 0);
}
#endif

return ngx_palloc_large(pool, size);

}

static ngx_inline void *
ngx_palloc_small(ngx_pool_t *pool, size_t size, ngx_uint_t align)
{
u_char *m;
ngx_pool_t *p;

p = pool->current;

do {
    m = p->d.last;

    if (align) {
        m = ngx_align_ptr(m, NGX_ALIGNMENT);
    }

    if ((size_t) (p->d.end - m) >= size) {
        p->d.last = m + size;

        return m;
    }

    p = p->d.next;

} while (p);

return ngx_palloc_block(pool, size);

}

static void *
ngx_palloc_block(ngx_pool_t *pool, size_t size)
{
u_char *m;
size_t psize;
ngx_pool_t *p, *new;

psize = (size_t) (pool->d.end - (u_char *) pool);

m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);
if (m == NULL) {
    return NULL;
}

new = (ngx_pool_t *) m;

new->d.end = m + psize;
new->d.next = NULL;
new->d.failed = 0;

m += sizeof(ngx_pool_data_t);
m = ngx_align_ptr(m, NGX_ALIGNMENT);
new->d.last = m + size;

for (p = pool->current; p->d.next; p = p->d.next) {
    if (p->d.failed++ > 4) {
        pool->current = p->d.next;
    }
}

p->d.next = new;

return m;

}

static void *
ngx_palloc_large(ngx_pool_t *pool, size_t size)
{
void *p;
ngx_uint_t n;
ngx_pool_large_t *large;

p = ngx_alloc(size, pool->log);
if (p == NULL) {
    return NULL;
}

n = 0;

for (large = pool->large; large; large = large->next) {
    if (large->alloc == NULL) {
        large->alloc = p;
        return p;
    }

    if (n++ > 3) {
        break;
    }
}

large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);
if (large == NULL) {
    ngx_free(p);
    return NULL;
}

large->alloc = p;
large->next = pool->large;
pool->large = large;

return p;

}

void *
ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment)
{
void *p;
ngx_pool_large_t *large;

p = ngx_memalign(alignment, size, pool->log);
if (p == NULL) {
    return NULL;
}

large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);
if (large == NULL) {
    ngx_free(p);
    return NULL;
}

large->alloc = p;
large->next = pool->large;
pool->large = large;

return p;

}

ngx_int_t
ngx_pfree(ngx_pool_t *pool, void *p)
{
ngx_pool_large_t *l;

for (l = pool->large; l; l = l->next) {
    if (p == l->alloc) {
        printf("free: %p\n", l->alloc);
        ngx_free(l->alloc);
        l->alloc = NULL;

        return NGX_OK;
    }
}

return NGX_DECLINED;

}

void *
ngx_pcalloc(ngx_pool_t *pool, size_t size)
{
void *p;

p = ngx_palloc(pool, size);
if (p) {
    ngx_memzero(p, size);
}

return p;

}

ngx_pool_cleanup_t *
ngx_pool_cleanup_add(ngx_pool_t *p, size_t size)
{
ngx_pool_cleanup_t *c;

c = ngx_palloc(p, sizeof(ngx_pool_cleanup_t));
if (c == NULL) {
    return NULL;
}

if (size) {
    c->data = ngx_palloc(p, size);
    if (c->data == NULL) {
        return NULL;
    }

} else {
    c->data = NULL;
}

c->handler = NULL;
c->next = p->cleanup;

p->cleanup = c;

printf("add cleanup: %p\n", c);

return c;

}

void
ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd)
{
ngx_pool_cleanup_t *c;
ngx_pool_cleanup_file_t *cf;

for (c = p->cleanup; c; c = c->next) {
    if (c->handler == ngx_pool_cleanup_file) {

        cf = c->data;

        if (cf->fd == fd) {
            c->handler(cf);
            c->handler = NULL;
            return;
        }
    }
}

}

void
ngx_pool_cleanup_file(void *data)
{
ngx_pool_cleanup_file_t *c = data;

printf("file cleanup: fd:%d\n",
               c->fd);

if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
    printf(" \"%s\" failed\n", c->name);
}

}

void
ngx_pool_delete_file(void *data)
{
ngx_pool_cleanup_file_t *c = data;

ngx_err_t  err;

printf("file cleanup: fd:%d %s\n",
               c->fd, c->name);

if (ngx_delete_file(c->name) == NGX_FILE_ERROR) {
    err = ngx_errno;

    if (err != NGX_ENOENT) {
        printf(" \"%s\" failed\n", c->name);
    }
}

if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
    printf(" \"%s\" failed\n", c->name);
}

}

#if 0

static void *
ngx_get_cached_block(size_t size)
{
void *p;
ngx_cached_block_slot_t *slot;

if (ngx_cycle->cache == NULL) {
    return NULL;
}

slot = &ngx_cycle->cache[(size + ngx_pagesize - 1) / ngx_pagesize];

slot->tries++;

if (slot->number) {
    p = slot->block;
    slot->block = slot->block->next;
    slot->number--;
    return p;
}

return NULL;

}

#endif
//-----------------------------------------------------------------------------------------
//array
typedef struct {
size_t len;
u_char *data;
} ngx_str_t;

#define ngx_memcpy(dst, src, n) (void) memcpy(dst, src, n)

#ifndef NGX_ARRAY_H_INCLUDED
#define NGX_ARRAY_H_INCLUDED

typedef struct {
void *elts;
ngx_uint_t nelts;
size_t size;
ngx_uint_t nalloc;
ngx_pool_t *pool;
} ngx_array_t;

ngx_array_t *ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size);
void ngx_array_destroy(ngx_array_t *a);
void *ngx_array_push(ngx_array_t *a);
void *ngx_array_push_n(ngx_array_t *a, ngx_uint_t n);

static ngx_inline ngx_int_t
ngx_array_init(ngx_array_t *array, ngx_pool_t pool, ngx_uint_t n, size_t size)
{
/

* set “array->nelts” before “array->elts”, otherwise MSVC thinks
* that “array->nelts” may be used without having been initialized
*/

array->nelts = 0;
array->size = size;
array->nalloc = n;
array->pool = pool;

array->elts = ngx_palloc(pool, n * size);
if (array->elts == NULL) {
    return NGX_ERROR;
}

return NGX_OK;

}

#endif /* NGX_ARRAY_H_INCLUDED */

ngx_array_t *
ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
{
ngx_array_t *a;

a = ngx_palloc(p, sizeof(ngx_array_t));
if (a == NULL) {
    return NULL;
}

if (ngx_array_init(a, p, n, size) != NGX_OK) {
    return NULL;
}

return a;

}

void
ngx_array_destroy(ngx_array_t *a)
{
ngx_pool_t *p;

p = a->pool;

if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) {
    p->d.last -= a->size * a->nalloc;
}

if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) {
    p->d.last = (u_char *) a;
}

}

void *
ngx_array_push(ngx_array_t *a)
{
void *elt, *new;
size_t size;
ngx_pool_t *p;

if (a->nelts == a->nalloc) {

    /* the array is full */

    size = a->size * a->nalloc;

    p = a->pool;

    if ((u_char *) a->elts + size == p->d.last
        && p->d.last + a->size <= p->d.end)
    {
        /*
         * the array allocation is the last in the pool
         * and there is space for new allocation
         */

        p->d.last += a->size;
        a->nalloc++;

    } else {
        /* allocate a new array */

        new = ngx_palloc(p, 2 * size);
        if (new == NULL) {
            return NULL;
        }

        ngx_memcpy(new, a->elts, size);
        a->elts = new;
        a->nalloc *= 2;
    }
}

elt = (u_char *) a->elts + a->size * a->nelts;
a->nelts++;

return elt;

}

void *
ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
{
void *elt, *new;
size_t size;
ngx_uint_t nalloc;
ngx_pool_t *p;

size = n * a->size;

if (a->nelts + n > a->nalloc) {

    /* the array is full */

    p = a->pool;

    if ((u_char *) a->elts + a->size * a->nalloc == p->d.last
        && p->d.last + size <= p->d.end)
    {
        /*
         * the array allocation is the last in the pool
         * and there is space for new allocation
         */

        p->d.last += size;
        a->nalloc += n;

    } else {
        /* allocate a new array */

        nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);

        new = ngx_palloc(p, nalloc * a->size);
        if (new == NULL) {
            return NULL;
        }

        ngx_memcpy(new, a->elts, a->nelts * a->size);
        a->elts = new;
        a->nalloc = nalloc;
    }
}

elt = (u_char *) a->elts + a->size * a->nelts;
a->nelts += n;

return elt;

}

//-------------------------------------------------------------------------------------------
//下面是Main函数和内存池打印代码

void print_pool(ngx_pool_t *pool)

{

if (pool->large != NULL)

{

    printf("has large memory\n");

    for(ngx_pool_large_t* i = pool->large; i!=NULL; i = i->next)

    {

        printf("\t\tlarge next=0x%x\n", i->next);

        printf("\t\tlarge alloc=0x%x\n", i->alloc);

    }

    }

    int i=1;

    while(pool)

    {

        printf("pool=0x%x,index:%d\n", pool, i++);

        printf("\t\tlast=0x%x\n", (pool->d).last);

        printf("\t\tend=0x%x\n",(pool->d).end);

        printf("\t\tnext=0x%x\n",(pool->d).next);

        printf("\t\tfailed=%d\n",pool->d.failed);

        printf("\t\tmax=%d\n",pool->max);

        printf("\t\tcurrent=0x%x\n",pool->current);

        printf("\t\tchain=0x%x\n",pool->chain);

        printf("\t\tlarge=0x%x\n",pool->large);

        printf("\t\tcleanup=0x%x\n",pool->cleanup);

        printf("\t\tlog=0x%x\n",pool->log);

        printf("\t\tavailable pool memory=%d\n", pool->d.end-pool->d.last);

        printf("\n");

        pool=pool->d.next;

    }

}

void print_array(int *a,int size)

{

for(int i=0; i<size; i++)

{

    printf("%d,",a[i]);

}

printf("\n");

}

int main()

{

ngx_pool_t *pool;

int array_size = 1;

int array_size_large = 1024;

int page_size = getpagesize();//获得一页的大小

printf("page_size:%d\n", page_size);

printf("----------------------------\n");

printf("create a new pool\n");

pool = ngx_create_pool(1024, NULL);//创建一个大小为1024的内存池

print_pool(pool);

printf("----------------------------\n");

printf("alloc block 1 from the pool:\n");

int *a1 = ngx_palloc(pool, sizeof(int) * array_size);//分配第一块内存 用于创建数组

for (int i=0; i< array_size; i++)

{

    a1[i] = i+1;

}

print_pool(pool);

printf("----------------------------\n");

printf("alloc block 2 from the pool:\n");

int *a2 = ngx_palloc(pool, sizeof(int) * array_size);//分配第二块内存 用于创建数组,这个时候会创建第二个内存池节点

for (int i=0; i< array_size; i++)

{

    a2[i] = 12345678;

}

print_pool(pool);

printf("----------------------------\n");

printf("alloc large memory:\n");

printf("\t\tlarge next before=0x%x\n", pool->current->d.last);

int * a3 = ngx_palloc(pool, sizeof(int) * array_size_large);//由于大小超过了max的值 ngx_palloc中会调用ngx_palloc_large分配大块内存

printf("\t\tlarge next after=0x%x\n", pool->large);

for (int i=0; i< array_size_large; i++)

{

    a3[i] = i+1;

}



print_array(a1,array_size);//分配的第一块内存块首地址

print_array(a2,array_size);//分配的第二块内存块首地址

// print_array(a3,array_size_large);//分配的大内存块首地址

//在内存数组中分三次写入 “hello”, “world”, “!”,然后使用指针加偏移,地址加数据长度两种方式将保存在数组中的文字打印出来
// char mychar[6] = “hello”;
// ngx_str_t mydata = {sizeof(mychar),mychar};
ngx_array_t *a = NULL;
a = ngx_array_create(pool, 1, sizeof(ngx_str_t));

ngx_str_t *s = ngx_array_push(a);
s->data = "hello";
s->len = sizeof(s->data);
printf("a->nelts: %ld, a->nalloc:%ld\n",a->nelts,a->nalloc);
printf("ngx_array_push:%s\n",((ngx_str_t *)(a->elts))->data);
printf("ngx_array_push:%s\n",s->data);

ngx_str_t *s1 = ngx_array_push(a);
s1->data = "wrold";
s1->len = sizeof(s1->data);
printf("a->nelts: %ld, a->nalloc:%ld\n",a->nelts,a->nalloc);
printf("ngx_array_push:%s\n",((ngx_str_t *)(a->elts+sizeof(ngx_str_t)))->data);//地址加数据长度
printf("ngx_array_push:%s\n",((ngx_str_t *)(a->elts)+1)->data); //使用指针加偏移
printf("ngx_array_push:%s\n",s1->data);

ngx_str_t *s2 = ngx_array_push(a);
s2->data = "!";
s2->len = sizeof(s2->data);
printf("a->nelts: %ld, a->nalloc:%ld\n",a->nelts,a->nalloc);
printf("ngx_array_push:%s\n",((ngx_str_t *)(a->elts+sizeof(ngx_str_t)*(a->nelts-1)))->data);//地址加数据长度
printf("ngx_array_push:%s\n",((ngx_str_t *)(a->elts)+a->nelts-1)->data); //使用指针加偏移
printf("ngx_array_push:%s\n",s2->data);

print_pool(pool);



ngx_destroy_pool(pool);

return 0;

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/898420.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C语言刷题训练DAY.8

1.计算单位阶跃函数 解题思路&#xff1a; 这个非常简单&#xff0c;只需要if else语句即可完成 解题代码&#xff1a; #include <stdio.h>int main() {int t 0;while(scanf("%d",&t)!EOF){if (t > 0)printf("1\n");else if (t < 0)pr…

LVS-DR集群(一台LVS,一台CIP,两台web,一台NFS)的构建以及LVS-DR模式工作原理和特点

一.LVS-DR工作模式原理和特点 1.工作模式 2.模式特点 二.构建环境 1.五台关闭防火墙&#xff0c;关闭selinux&#xff0c;拥有固定IP&#xff0c;部署有http服务的虚拟机&#xff0c;LVS设备下载ipvsadm工具&#xff0c;NFS 设备需要下载rpcbind和nfs-utils 2.实现功能 3…

win11调整屏幕亮度

1.右键打开 2.显示更多选项 3.NVIDIA控制面板 4.调整桌面颜色设置 5.亮度

linux 搭建 nexus maven私服

环境&#xff1a; 必须在 linux 环境下&#xff0c;并且已安装 jdk 下载 访问百度网盘链接: https://pan.baidu.com/s/1fHGmQ2jRUAsXyPom2KL8Mw?pwd0000 提取码: 0000 官网下载 Download Archives - Repository Manager 3 (sonatype.com) 部署 &#xff1a; 进入目录&#…

Echarts:象形柱图实现水塔水位的动画、水球图和液位柱子图

一、象形柱图 1、vue中使用象形柱图 效果图&#xff1a; 2、代码实现 <template><div :class"className" :style"{height:height,width:width}"/></template> <script>import echarts from echarts require(echarts/theme/macar…

论文浅尝 | KRACL-利用图上下文和对比学习的稀疏KG补全

笔记整理&#xff1a;李娟&#xff0c;浙江大学博士&#xff0c;研究方向为知识图谱表示学习 论文链接&#xff1a;https://arxiv.org/pdf/2208.07622.pdf 代码链接&#xff1a;https://github.com/TamSiuhin/KRACL 介绍 知识图谱&#xff08;KG&#xff09;通常是不完整的&…

注意力机制——SENet原理详解及源码解析

&#x1f34a;作者简介&#xff1a;秃头小苏&#xff0c;致力于用最通俗的语言描述问题 &#x1f34a;专栏推荐&#xff1a;深度学习网络原理与实战 &#x1f34a;近期目标&#xff1a;写好专栏的每一篇文章 &#x1f34a;支持小苏&#xff1a;点赞&#x1f44d;&#x1f3fc;、…

【ROS】话题通信--从理论介绍到模型实现(C++)

1.简单介绍 话题通信是ROS中使用频率最高的一种通信模式&#xff0c;话题通信是基于发布订阅模式的&#xff0c;也即:一个节点发布消息&#xff0c;另一个节点订阅该消息。像雷达、摄像头、GPS… 等等一些传感器数据的采集&#xff0c;也都是使用了话题通信&#xff0c;换言之…

windows 配置 Kerberos客户端访问CDH组件

0.背景 想在window机器上访问内网集群的CDH组件(如solr的webui),由于集群配置了Kerberos验证,所以需要配置相关,否则打开webui会有401未授权错误 1. 流程 1.1 windows安装Kerberos客户端 -下载 Windows系统客户端去下面网站按需下载 http://web.mit.edu/kerberos/dist/ 需要…

【力扣】496. 下一个更大元素 I <单调栈、模拟>

【力扣】496. 下一个更大元素 I nums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。给你两个没有重复元素的数组 nums1 和 nums2 &#xff0c;下标从 0 开始计数&#xff0c;其中nums1 是 nums2 的子集。   对于每个 0 < i <…

喜报 | 擎创再度入围IDC中国FinTech 50榜单

8月16日&#xff0c;2023年度“IDC中国FinTech 50”榜单正式揭晓&#xff0c;擎创科技继2022年入选该榜单后&#xff0c;再次以创新者姿态成功入选&#xff0c;并以技术赋能业务创新&#xff0c;成为中国金融科技领域创新与活力的重要贡献者。 “IDC中国FinTech 50”旨在评选出…

RabbitMq交换机类型介绍

RabbitMq交换机类型介绍 在RabbitMq中&#xff0c;生产者的消息都是通过交换器来接收&#xff0c;然后再从交换器分发到不同的队列&#xff0c;再由消费者从队列获取消息。这种模式也被成为“发布/订阅”。 分发的过程中交换器类型会影响分发的逻辑。 直连交换机&#xff1a…

高校大学生社团管理系统的设计与实现(论文+源码)_kaic

目 录 一、绪论 &#xff08;一&#xff09;选题背景 1、社团管理系统的提出 &#xff08;二&#xff09;系统设计的原则与目标 1、系统设计原则 2、系统设计目标 二、系统关键技术的分析 &#xff08;一&#xff09;JSP技术 &#xff08;二&#xff09;Tomcat简介 1、SERVL…

虚拟机远程连接出现 Connection refused: connect问题(已解决)

如果你也跟我一样出现了这样的问题&#xff0c;并且网上的方法试了都不行&#xff0c;不妨试试我的简单粗暴的方法&#xff0c; 那就是拔网线&#xff0c;我的就是拔网线&#xff0c;重新连接就行了&#xff0c;佛了

Creating a document in Overleaf

1、Uploading a project上传项目 This artcle provides a step-by-step guide showing how to create an Overleaf project by uploading a .zip file containing LaTeX files stored on your local computer 1、Create a .zip file containing your local files (images, bib…

如何优雅的进行接口设计?接口设计的六大原则是什么?

如何优雅的进行接口设计&#xff1f;接口设计的六大原则是什么&#xff1f; 本文关于如何优雅的进行接口设计&#xff1f;接口设计的六大原则是什么&#xff1f; 作为后端开发&#xff0c;不管是什么语言&#xff0c;Java、Go还是C&#xff0c;其背后的后端思想都是类似的。后…

JAVA免杀学习与实验

1 认识Webshell 创建一个JSP文件&#xff1a; <% page import"java.io.InputStream" %> <% page import"java.io.BufferedReader" %> <% page import"java.io.InputStreamReader" %> <% page language"java" p…

【linux基础(四)】对Linux权限的理解

&#x1f493;博主CSDN主页:杭电码农-NEO&#x1f493;   ⏩专栏分类:Linux从入门到开通⏪   &#x1f69a;代码仓库:NEO的学习日记&#x1f69a;   &#x1f339;关注我&#x1faf5;带你学更多操作系统知识   &#x1f51d;&#x1f51d; Linux权限 1. 前言2. shell命…

Linux 进程间通信——共享内存

一、共享内存原理 共享内存为多个进程之间共享和传递数据提供了一种有效的方式。共享内存是先在物理内存上申请一块空间&#xff0c;多个进程可以将其映射到自己的虚拟地址空间中。所有进程都可以访问共享内存中的地址&#xff0c;就好像它们是由malloc分配的一样。如果某个进…