nginx 0.8.26
[nginx.git] / src / os / unix / ngx_alloc.c
blobc71a10254c6d17aef31ab53c1c8ac8a39867c0bb
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
11 ngx_uint_t ngx_pagesize;
12 ngx_uint_t ngx_pagesize_shift;
13 ngx_uint_t ngx_cacheline_size;
16 void *
17 ngx_alloc(size_t size, ngx_log_t *log)
19 void *p;
21 p = malloc(size);
22 if (p == NULL) {
23 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
24 "malloc(%uz) failed", size);
27 ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, "malloc: %p:%uz", p, size);
29 return p;
33 void *
34 ngx_calloc(size_t size, ngx_log_t *log)
36 void *p;
38 p = ngx_alloc(size, log);
40 if (p) {
41 ngx_memzero(p, size);
44 return p;
48 #if (NGX_HAVE_POSIX_MEMALIGN)
50 void *
51 ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
53 void *p;
54 int err;
56 err = posix_memalign(&p, alignment, size);
58 if (err) {
59 ngx_log_error(NGX_LOG_EMERG, log, err,
60 "posix_memalign(%uz, %uz) failed", alignment, size);
61 p = NULL;
64 ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, log, 0,
65 "posix_memalign: %p:%uz @%uz", p, size, alignment);
67 return p;
70 #elif (NGX_HAVE_MEMALIGN)
72 void *
73 ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
75 void *p;
77 p = memalign(alignment, size);
78 if (p == NULL) {
79 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
80 "memalign(%uz, %uz) failed", alignment, size);
83 ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, log, 0,
84 "memalign: %p:%uz @%uz", p, size, alignment);
86 return p;
89 #endif