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