Skip to content

Commit 3b393b2

Browse files
committed
new carray capi version
1 parent 6eab4ae commit 3b393b2

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

src/carray_capi.h

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#endif
1212

1313
#define CARRAY_CAPI_ID_STRING "_capi_carray"
14-
#define CARRAY_CAPI_VERSION_MAJOR -2
14+
#define CARRAY_CAPI_VERSION_MAJOR 1
1515
#define CARRAY_CAPI_VERSION_MINOR 0
1616
#define CARRAY_CAPI_VERSION_PATCH 0
1717

@@ -77,7 +77,7 @@ enum carray_attr
7777

7878
struct carray_info
7979
{
80-
carray_type type;
80+
carray_type elementType;
8181
carray_attr attr;
8282
size_t elementSize;
8383
size_t elementCount;
@@ -206,13 +206,55 @@ struct carray_capi
206206
* newElementCount - new number of elements. If this is larger than the current
207207
* element count, the new elements are uninitialized.
208208
*
209-
* shrinkCapacity - flag, if true the capacity is set to the new size.
209+
* reservePercent - if < 0 the capacity is set to the new size,
210+
* if == 0 the capacity remains if the new size is smaller
211+
* than current capacity.
212+
* if > 0 the capacity gets a reserve of reservePercent percent
213+
* if capacity needs to be increased. E.g. if reservePercent == 100
214+
* the capacity is doubled if newElementCount exceeds the current
215+
* capacity.
210216
*
211217
* Returns pointer to the first element in the array. The caller may read
212218
* or write newElementCount elements at this pointer.
213219
* Returns NULL on failure or if newElementCount == 0.
214220
*/
215-
void* (*resizeCarray)(carray* a, size_t newElementCount, int shrinkCapacity);
221+
void* (*resizeCarray)(carray* a, size_t newElementCount, int reservePercent);
222+
223+
/**
224+
* Insert Elements.
225+
*
226+
* offset - index of the first new element, 0 <= offset <= elementCount
227+
* insertCount - number of elements to be inserted
228+
*
229+
* reservePercent - if < 0 the capacity is set to the new size,
230+
* if == 0 the capacity remains if the new size is smaller
231+
* than current capacity.
232+
* if > 0 the capacity gets a reserve of reservePercent percent
233+
* if capacity needs to be increased. E.g. if reservePercent == 100
234+
* the capacity is doubled if new element count exceeds the current
235+
* capacity.
236+
*
237+
* Returns pointer to the first new element in the array. The caller may write
238+
* insertCount elements at this pointer.
239+
* Returns NULL on failure or if insertCount == 0.
240+
*/
241+
void* (*insertElements)(carray* a, size_t offset, size_t insertCount, int reservePercent);
242+
243+
/**
244+
* Insert Elements.
245+
*
246+
* offset - index of the first element to be removed, 0 <= offset < elementCount
247+
* removeCount - number of elements to be removed
248+
*
249+
* reservePercent - if < 0 the capacity is set to the new size,
250+
* if == 0 the capacity remains if the new size is smaller
251+
* than current capacity.
252+
* if > 0 the capacity gets a reserve of reservePercent percent
253+
* if capacity needs to be increased. E.g. if reservePercent == 100
254+
* the capacity is doubled if new element count exceeds the current
255+
* capacity.
256+
*/
257+
void (*removeElements)(carray* a, size_t offset, size_t count, int reservePercent);
216258
};
217259

218260
#if CARRAY_CAPI_IMPLEMENT_SET_CAPI

src/receiver_capi_impl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static void* addArrayToWriter(receiver_writer* writer, receiver_array_type type,
161161
default: return NULL;
162162
}
163163
carray_info info = {0};
164-
info.type = carrayType;
164+
info.elementType = carrayType;
165165
info.elementSize = elementSize;
166166
info.elementCount = elementCount;
167167
size_t args_size = mtmsg_serialize_calc_carray_size(&info);

src/serialize.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ int mtmsg_serialize_get_msg_args(lua_State* L)
195195
par->errorArg = arg;
196196
return raiseCarrayError(L, capi, reason);
197197
}
198-
capi->resizeCarray(carray, 0, false);
198+
capi->resizeCarray(carray, 0, 0);
199199
}
200200
arg += 1;
201201
}
@@ -263,7 +263,7 @@ int mtmsg_serialize_get_msg_args(lua_State* L)
263263
break;
264264
}
265265
case BUFFER_CARRAY: {
266-
carray_type type = (unsigned char)buffer[p++];
266+
carray_type elementType = (unsigned char)buffer[p++];
267267
unsigned char elementSize = (unsigned char)buffer[p++];;
268268
size_t elementCount;
269269
memcpy(&elementCount, buffer + p, sizeof(size_t));
@@ -285,10 +285,10 @@ int mtmsg_serialize_get_msg_args(lua_State* L)
285285
return raiseCarrayError(L, capi, reason);
286286
}
287287
arg += 1;
288-
if (info.type == type && info.elementSize == elementSize) {
288+
if (info.elementType == elementType && info.elementSize == elementSize) {
289289
break;
290290
}
291-
capi->resizeCarray(carray, 0, false);
291+
capi->resizeCarray(carray, 0, 0);
292292
capi = NULL;
293293
carray = NULL;
294294
} else {
@@ -297,7 +297,7 @@ int mtmsg_serialize_get_msg_args(lua_State* L)
297297
}
298298
void* data;
299299
if (carray) {
300-
data = capi->resizeCarray(carray, elementCount, false);
300+
data = capi->resizeCarray(carray, elementCount, 0);
301301
if (!data) {
302302
par->errorArg = arg - 1;
303303
return luaL_error(L, "cannot resize carray");
@@ -307,8 +307,8 @@ int mtmsg_serialize_get_msg_args(lua_State* L)
307307
if (!par->carrayCapi) {
308308
par->carrayCapi = carray_require_capi(L);
309309
}
310-
if (!par->carrayCapi->newCarray(L, type, CARRAY_DEFAULT, elementCount, &data)) {
311-
return luaL_error(L, "internal error creating carray for type %d", type);
310+
if (!par->carrayCapi->newCarray(L, elementType, CARRAY_DEFAULT, elementCount, &data)) {
311+
return luaL_error(L, "internal error creating carray for type %d", elementType);
312312
}
313313
}
314314
memcpy(data, buffer + p, len);

src/serialize.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static inline char* mtmsg_serialize_cfunction_to_buffer(lua_CFunction value, cha
129129
static inline char* mtmsg_serialize_carray_header_to_buffer(carray_info* info, char* buffer)
130130
{
131131
*buffer++ = BUFFER_CARRAY;
132-
*buffer++ = info->type;
132+
*buffer++ = info->elementType;
133133
*buffer++ = (unsigned char)info->elementSize;
134134
memcpy(buffer, &info->elementCount, sizeof(size_t));
135135
buffer += sizeof(size_t);

0 commit comments

Comments
 (0)