Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
| 8 | #include <ngx_core.h> |
| 9 | |
| 10 | |
| 11 | int ngx_pagesize; |
| 12 | |
| 13 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame^] | 14 | void * |
| 15 | ngx_alloc(size_t size, ngx_log_t *log) |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 16 | { |
| 17 | void *p; |
| 18 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame^] | 19 | p = malloc(size); |
| 20 | if (p == NULL) { |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 21 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 22 | "malloc() %uz bytes failed", size); |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 25 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, "malloc: %p:%uz", p, size); |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 26 | |
| 27 | return p; |
| 28 | } |
| 29 | |
| 30 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame^] | 31 | void * |
| 32 | ngx_calloc(size_t size, ngx_log_t *log) |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 33 | { |
| 34 | void *p; |
| 35 | |
| 36 | p = ngx_alloc(size, log); |
| 37 | |
| 38 | if (p) { |
| 39 | ngx_memzero(p, size); |
| 40 | } |
| 41 | |
| 42 | return p; |
| 43 | } |
| 44 | |
| 45 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 46 | #if (NGX_HAVE_POSIX_MEMALIGN) |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 47 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame^] | 48 | void * |
| 49 | ngx_memalign(size_t alignment, size_t size, ngx_log_t *log) |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 50 | { |
| 51 | void *p; |
| 52 | |
Igor Sysoev | 3eb1995 | 2004-06-08 16:26:37 +0000 | [diff] [blame] | 53 | if (posix_memalign(&p, alignment, size) == -1) { |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 54 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 55 | "posix_memalign() %uz bytes aligned to %uz failed", |
| 56 | size, alignment); |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 60 | "posix_memalign: %p:%uz", p, size); |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 61 | |
| 62 | return p; |
| 63 | } |
| 64 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 65 | #elif (NGX_HAVE_MEMALIGN) |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 66 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame^] | 67 | void * |
| 68 | ngx_memalign(size_t alignment, size_t size, ngx_log_t *log) |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 69 | { |
| 70 | void *p; |
| 71 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame^] | 72 | p = memalign(alignment, size); |
| 73 | if (p == NULL) { |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 74 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 75 | "memalign() %uz bytes aligned to %uz failed", |
| 76 | size, alignment); |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 80 | "memalign: %p:%uz", p, size); |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 81 | |
| 82 | return p; |
| 83 | } |
| 84 | |
| 85 | #endif |