OpenDNSSEC-signer  1.4.7
allocator.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2011 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
32 #include "config.h"
33 #include "shared/allocator.h"
34 #include "shared/log.h"
35 
36 #include <stdlib.h>
37 #include <string.h>
38 
39 static const char* allocator_str = "allocator";
40 
41 
47 allocator_create(void *(*allocator)(size_t size), void (*deallocator)(void *))
48 {
49  allocator_type* result =
50  (allocator_type*) allocator(sizeof(allocator_type));
51  if (!result) {
52  ods_log_error("[%s] failed to create allocator", allocator_str);
53  return NULL;
54  }
55  result->allocator = allocator;
56  result->deallocator = deallocator;
57  return result;
58 }
59 
60 
65 void*
66 allocator_alloc(allocator_type* allocator, size_t size)
67 {
68  void* result;
69 
70  ods_log_assert(allocator);
71  /* align size */
72  if (size == 0) {
73  size = 1;
74  }
75  result = allocator->allocator(size);
76  if (!result) {
77  ods_fatal_exit("[%s] allocator failed: out of memory", allocator_str);
78  return NULL;
79  }
80  return result;
81 }
82 
83 
88 void*
89 allocator_alloc_zero(allocator_type *allocator, size_t size)
90 {
91  void *result = allocator_alloc(allocator, size);
92  if (!result) {
93  return NULL;
94  }
95  memset(result, 0, size);
96  return result;
97 }
98 
99 
104 void*
105 allocator_alloc_init(allocator_type *allocator, size_t size, const void *init)
106 {
107  void *result = allocator_alloc(allocator, size);
108  if (!result) {
109  return NULL;
110  }
111  memcpy(result, init, size);
112  return result;
113 }
114 
115 
120 char*
121 allocator_strdup(allocator_type *allocator, const char *string)
122 {
123  if (!string) {
124  return NULL;
125  }
126  return (char*) allocator_alloc_init(allocator, strlen(string) + 1, string);
127 }
128 
129 
134 void
135 allocator_deallocate(allocator_type *allocator, void* data)
136 {
137  ods_log_assert(allocator);
138  if (!data) {
139  return;
140  }
141  allocator->deallocator(data);
142  return;
143 }
144 
145 
150 void
152 {
153  void (*deallocator)(void *);
154  if (!allocator) {
155  return;
156  }
157  deallocator = allocator->deallocator;
158  deallocator(allocator);
159  return;
160 }
161 
void * allocator_alloc_zero(allocator_type *allocator, size_t size)
Definition: allocator.c:89
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:66
void ods_fatal_exit(const char *format,...)
Definition: log.c:382
void ods_log_error(const char *format,...)
Definition: log.c:334
allocator_type * allocator_create(void *(*allocator)(size_t size), void(*deallocator)(void *))
Definition: allocator.c:47
void *(* allocator)(size_t)
Definition: allocator.h:40
char * allocator_strdup(allocator_type *allocator, const char *string)
Definition: allocator.c:121
void allocator_cleanup(allocator_type *allocator)
Definition: allocator.c:151
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:135
void * allocator_alloc_init(allocator_type *allocator, size_t size, const void *init)
Definition: allocator.c:105
void(* deallocator)(void *)
Definition: allocator.h:41
#define ods_log_assert(x)
Definition: log.h:154