Skip to content
This repository
Newer
Older
100644 166 lines (157 sloc) 5.581 kb
91b4e62b » okeuday
2011-03-27 Make the NIF more robust. Guard non-reentrant ZeroMQ sockets. Provide…
1 /* -*- coding: utf-8; Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 * ex: set softtabstop=4 tabstop=4 shiftwidth=4 expandtab fileencoding=utf-8:
3 *
4 * BSD LICENSE
5 *
6 * Copyright (c) 2011, Michael Truog <mjtruog at gmail dot com>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * * All advertising materials mentioning features or use of this
19 * software must display the following acknowledgment:
20 * This product includes software developed by Michael Truog
21 * * The name of the author may not be used to endorse or promote
22 * products derived from this software without specific prior
23 * written permission
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38 * DAMAGE.
39 */
40 #include "vector.h"
41 #include <string.h>
42 #include <stdlib.h>
43
44 static size_t greater_pow2(size_t size);
45
46 int vector_initialize(vector_t * v,
47 size_t allocation_increment,
48 size_t allocation_size,
49 size_t allocation_size_max,
50 size_t element_size)
51 {
52 v->allocation_size = greater_pow2(allocation_size);
53 v->allocation_increment = greater_pow2(allocation_increment);
54 v->allocation_size_max = allocation_size_max;
55 v->p = malloc(v->allocation_size);
56 v->element_size = element_size;
57 v->count = 0;
58 if (v->p)
59 return VECTOR_SUCCESS;
60 else
61 return VECTOR_FAILURE;
62 }
63
64 void vector_destroy(vector_t * v)
65 {
66 free(v->p);
67 }
68
69 int vector_reserve(vector_t * v, size_t count)
70 {
71 size_t const size = v->element_size * count;
72 size_t new_size = v->allocation_size;
73 void * tmp;
74 if (size < v->allocation_size)
75 return VECTOR_SUCCESS;
76 if (size > v->allocation_size_max)
77 return VECTOR_FAILURE;
78 if (v->allocation_increment == 0) {
79 while (size > new_size)
80 new_size <<= 1;
81 }
82 else {
83 while (size > new_size)
84 new_size += v->allocation_increment;
85 }
86 if (new_size > v->allocation_size_max)
87 new_size = v->allocation_size_max;
88 tmp = realloc(v->p, new_size);
89 if (! tmp)
90 return VECTOR_FAILURE;
91 v->p = tmp;
92 v->allocation_size = new_size;
93 return VECTOR_SUCCESS;
94 }
95
96 int vector_copy(vector_t * v_dst, vector_t * v_src,
97 size_t i_src, size_t count_src, size_t i_dst)
98 {
99 if (v_dst->element_size != v_src->element_size)
100 return VECTOR_FAILURE;
101 if (count_src == 0)
102 count_src = v_src->count;
103 if (vector_reserve(v_dst, i_dst + count_src) == VECTOR_FAILURE)
104 return VECTOR_FAILURE;
105 memcpy(&(((char *) v_dst->p)[i_dst * v_dst->element_size]),
106 &(((char *) v_src->p)[i_src * v_dst->element_size]),
107 count_src * v_dst->element_size);
108 if ((i_dst + count_src) > v_dst->count)
109 v_dst->count = i_dst + count_src;
110 return VECTOR_SUCCESS;
111 }
112
113 int vector_move(vector_t * v, size_t i_dst, size_t i_src, size_t count_src)
114 {
115 if (count_src == 0)
116 count_src = v->count - i_src;
117 if (vector_reserve(v, i_dst + count_src) == VECTOR_FAILURE)
118 return VECTOR_FAILURE;
119 memmove(&(((char *) v->p)[i_dst * v->element_size]),
120 &(((char *) v->p)[i_src * v->element_size]),
121 count_src * v->element_size);
122 if ((i_dst + count_src) > v->count)
123 v->count = i_dst + count_src;
124 return VECTOR_SUCCESS;
125 }
126
127 int vector_append_element(vector_t * v, void * p, size_t size)
128 {
129 if (v->element_size != size)
130 return VECTOR_FAILURE;
131 if (vector_reserve(v, v->count + 1) == VECTOR_FAILURE)
132 return VECTOR_FAILURE;
133 memcpy(&(((char *) v->p)[v->count * v->element_size]), p, size);
134 ++(v->count);
135 return VECTOR_SUCCESS;
136 }
137
138 int vector_remove(vector_t * v, size_t i)
139 {
140 if ((i + 1) == v->count) {
141 --(v->count);
142 return VECTOR_SUCCESS;
143 }
144 else {
145 if (vector_move(v, i, i + 1, 0) == VECTOR_FAILURE)
146 return VECTOR_FAILURE;
147 --(v->count);
148 return VECTOR_SUCCESS;
149 }
150 }
151
152 /* find a value >= totalSize as a power of 2
153 */
154 static size_t greater_pow2(size_t total_size)
155 {
156 int bits = 0;
157 size_t div2 = total_size;
158 size_t value;
159 for (; div2 > 1; div2 >>= 1)
160 bits++;
161 value = (1 << bits);
162 if (value == total_size)
163 return value;
164 else
165 return (value << 1);
166 }
167
Something went wrong with that request. Please try again.