CUBRID Engine  latest
dynamic_array.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * dynamic_array.c
21  */
22 
23 #include <string.h>
24 #include <malloc.h>
25 
26 #include "error_code.h"
27 #include "dynamic_array.h"
28 
30 da_create (int count, size_t len)
31 {
32  dynamic_array *da = (dynamic_array *) malloc (sizeof (dynamic_array));
33  if (da == NULL)
34  {
35  return NULL;
36  }
37 
38  if (count == 0)
39  {
40  count = 1000;
41  }
42 
43  da->max = -1;
44  da->len = (int) len;
45  da->count = count;
46  da->array = (unsigned char *) calloc (len, count);
47  if (da->array == NULL)
48  {
49  free (da);
50  return NULL;
51  }
52 
53  return da;
54 }
55 
56 static int
58 {
59  int count = da->count * 2;
60  int i;
61 
62  while (max >= count)
63  {
64  count *= 2;
65  }
66 
67  da->array = (unsigned char *) realloc (da->array, da->len * count);
68  if (da->array == NULL)
69  {
70  return ER_FAILED;
71  }
72 
73  for (i = da->count * da->len; i < count * da->len; i++)
74  {
75  da->array[i] = 0;
76  }
77  da->count = count;
78  return NO_ERROR;
79 }
80 
81 int
82 da_add (dynamic_array * da, void *data)
83 {
84  int pos = da->max + 1;
85  return da_put (da, pos, data);
86 }
87 
88 int
89 da_put (dynamic_array * da, int pos, void *data)
90 {
91  if (pos >= da->count && da_expand (da, pos) != NO_ERROR)
92  {
93  return ER_FAILED;
94  }
95 
96  if (pos > da->max)
97  {
98  da->max = pos;
99  }
100 
101  memcpy (&(da->array[pos * da->len]), data, da->len);
102  return NO_ERROR;
103 }
104 
105 int
106 da_get (dynamic_array * da, int pos, void *data)
107 {
108  if (pos > da->max)
109  {
110  return ER_FAILED;
111  }
112 
113  memcpy (data, &(da->array[pos * da->len]), da->len);
114  return NO_ERROR;
115 }
116 
117 int
119 {
120  if (da == NULL)
121  {
122  return 0;
123  }
124 
125  return da->max + 1;
126 }
127 
128 int
130 {
131  if (da)
132  {
133  if (da->array)
134  {
135  free (da->array);
136  }
137  free (da);
138  }
139 
140  return NO_ERROR;
141 }
#define NO_ERROR
Definition: error_code.h:46
int da_destroy(dynamic_array *da)
#define ER_FAILED
Definition: error_code.h:47
unsigned char * array
Definition: dynamic_array.h:32
int da_put(dynamic_array *da, int pos, void *data)
Definition: dynamic_array.c:89
int da_get(dynamic_array *da, int pos, void *data)
int da_size(dynamic_array *da)
static int da_expand(dynamic_array *da, int max)
Definition: dynamic_array.c:57
dynamic_array * da_create(int count, size_t len)
Definition: dynamic_array.c:30
#define NULL
Definition: freelistheap.h:34
int count(int &result, const cub_regex_object &reg, const std::string &src, const int position, const INTL_CODESET codeset)
#define max(a, b)
int da_add(dynamic_array *da, void *data)
Definition: dynamic_array.c:82
int i
Definition: dynamic_load.c:954