blob: 72cf4e42ecf52555def447b1a9c9cb3da2ed81f9 [file] [log] [blame]
Igor Sysoev0ab91b92004-06-06 19:49:18 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev0ab91b92004-06-06 19:49:18 +00007#include <ngx_config.h>
8#include <ngx_core.h>
9
10
11int ngx_pagesize;
12
13
Igor Sysoevc1571722005-03-19 12:38:37 +000014void *
15ngx_alloc(size_t size, ngx_log_t *log)
Igor Sysoev0ab91b92004-06-06 19:49:18 +000016{
17 void *p;
18
Igor Sysoevc1571722005-03-19 12:38:37 +000019 p = malloc(size);
20 if (p == NULL) {
Igor Sysoev0ab91b92004-06-06 19:49:18 +000021 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
Igor Sysoev1b735832004-11-11 14:07:14 +000022 "malloc() %uz bytes failed", size);
Igor Sysoev0ab91b92004-06-06 19:49:18 +000023 }
24
Igor Sysoev1b735832004-11-11 14:07:14 +000025 ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, "malloc: %p:%uz", p, size);
Igor Sysoev0ab91b92004-06-06 19:49:18 +000026
27 return p;
28}
29
30
Igor Sysoevc1571722005-03-19 12:38:37 +000031void *
32ngx_calloc(size_t size, ngx_log_t *log)
Igor Sysoev0ab91b92004-06-06 19:49:18 +000033{
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 Sysoevf6906042004-11-25 16:17:31 +000046#if (NGX_HAVE_POSIX_MEMALIGN)
Igor Sysoev0ab91b92004-06-06 19:49:18 +000047
Igor Sysoevc1571722005-03-19 12:38:37 +000048void *
49ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
Igor Sysoev0ab91b92004-06-06 19:49:18 +000050{
51 void *p;
52
Igor Sysoev3eb19952004-06-08 16:26:37 +000053 if (posix_memalign(&p, alignment, size) == -1) {
Igor Sysoev0ab91b92004-06-06 19:49:18 +000054 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
Igor Sysoev1b735832004-11-11 14:07:14 +000055 "posix_memalign() %uz bytes aligned to %uz failed",
56 size, alignment);
Igor Sysoev0ab91b92004-06-06 19:49:18 +000057 }
58
59 ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +000060 "posix_memalign: %p:%uz", p, size);
Igor Sysoev0ab91b92004-06-06 19:49:18 +000061
62 return p;
63}
64
Igor Sysoevf6906042004-11-25 16:17:31 +000065#elif (NGX_HAVE_MEMALIGN)
Igor Sysoev0ab91b92004-06-06 19:49:18 +000066
Igor Sysoevc1571722005-03-19 12:38:37 +000067void *
68ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
Igor Sysoev0ab91b92004-06-06 19:49:18 +000069{
70 void *p;
71
Igor Sysoevc1571722005-03-19 12:38:37 +000072 p = memalign(alignment, size);
73 if (p == NULL) {
Igor Sysoev0ab91b92004-06-06 19:49:18 +000074 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
Igor Sysoev1b735832004-11-11 14:07:14 +000075 "memalign() %uz bytes aligned to %uz failed",
76 size, alignment);
Igor Sysoev0ab91b92004-06-06 19:49:18 +000077 }
78
79 ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +000080 "memalign: %p:%uz", p, size);
Igor Sysoev0ab91b92004-06-06 19:49:18 +000081
82 return p;
83}
84
85#endif