Commit 63c120eb authored by Christoffer Ackelman's avatar Christoffer Ackelman

Added documentation to co_array.

parent 852ae8d7
......@@ -1225,7 +1225,6 @@ static void classList(qcom_sGet* get)
int i;
pwr_tOid oid;
int listcnt;
array_tCtx arr;
pwr_tAttrRef aref;
gdb_ScopeLock
......@@ -1239,14 +1238,14 @@ static void classList(qcom_sGet* get)
cdh_NodeIdToString(NULL, np->nid, 0, 0));
}
array_Init(&arr, sizeof(pwr_tAttrRef), 20);
array_tCtx arr = array_New(sizeof(pwr_tAttrRef), 20);
listcnt = 0;
if (mp->attrobjects) {
for (i = 0; i < mp->cidcnt; i++) {
for (sts = gdh_GetClassListAttrRef(mp->cid[i], &aref); ODD(sts);
sts = gdh_GetNextAttrRef(mp->cid[i], &aref, &aref)) {
array_Add(arr, &aref);
array_Push(arr, &aref);
listcnt++;
}
}
......@@ -1255,7 +1254,7 @@ static void classList(qcom_sGet* get)
for (sts = gdh_GetClassList(mp->cid[i], &oid); ODD(sts);
sts = gdh_GetNextObject(oid, &oid)) {
aref = cdh_ObjidToAref(oid);
array_Add(arr, &aref);
array_Push(arr, &aref);
listcnt++;
}
}
......@@ -1279,10 +1278,10 @@ static void classList(qcom_sGet* get)
rmp->listcnt = 0;
} else {
rmp->listcnt = listcnt;
memcpy(rmp->classlist, arr->a, listcnt * sizeof(pwr_tAttrRef));
memcpy(rmp->classlist, arr->data, listcnt * sizeof(pwr_tAttrRef));
}
array_Close(arr);
array_Delete(arr);
net_Reply(&sts, get, &put, 0);
}
......@@ -38,90 +38,51 @@
#include "pwr.h"
#include "co_array.h"
#include "co_math.h"
int array_Init(array_tCtx* ctx, int elemsize, int alloc_incr)
array_tCtx array_New(int elemsize, int alloc_incr)
{
*ctx = calloc(1, sizeof(array_sCtx));
if (!*ctx)
return 0;
(*ctx)->elemsize = elemsize;
(*ctx)->alloc_incr = alloc_incr;
(*ctx)->a = calloc(1, elemsize * alloc_incr);
if (!(*ctx)->a)
return 0;
(*ctx)->allocated = alloc_incr;
return 1;
array_tCtx arr = calloc(1, sizeof(array_sCtx));
if (!arr)
return NULL;
arr->data = calloc(1, elemsize * alloc_incr);
if (!arr->data)
return NULL;
arr->elemsize = elemsize;
arr->alloc_incr = alloc_incr;
arr->capacity = alloc_incr;
return arr;
}
void array_Close(array_tCtx ctx)
void array_Delete(array_tCtx arr)
{
free((char*)ctx->a);
free((char*)ctx);
free((char*)arr->data);
free((char*)arr);
}
int array_Add(array_tCtx ctx, void* elem)
int array_Push(array_tCtx arr, void* elem)
{
if (ctx->allocated <= ctx->a_size) {
void* a_tmp;
ctx->allocated += ctx->alloc_incr;
a_tmp = calloc(ctx->allocated, ctx->elemsize);
if (!a_tmp)
return 0;
memcpy(a_tmp, ctx->a, ctx->a_size * ctx->elemsize);
free((char*)ctx->a);
ctx->a = a_tmp;
}
memcpy((char*)ctx->a + ctx->a_size * ctx->elemsize, elem, ctx->elemsize);
ctx->a_size++;
return 1;
return array_Concat(arr, elem, 1);
}
int array_MAdd(array_tCtx ctx, void* elem, int elements)
int array_Concat(array_tCtx arr, void* elems, int number)
{
if (ctx->allocated <= ctx->a_size + elements - 1) {
void* a_tmp;
ctx->allocated += ctx->alloc_incr;
if (ctx->allocated < ctx->a_size + elements)
ctx->allocated = ctx->a_size + elements;
a_tmp = calloc(ctx->allocated, ctx->elemsize);
if (arr->capacity <= arr->size + number - 1) {
void* a_tmp = realloc(arr->data, arr->elemsize * MAX(arr->capacity + arr->alloc_incr, arr->size + number));
if (!a_tmp)
return 0;
memcpy(a_tmp, ctx->a, ctx->a_size * ctx->elemsize);
free((char*)ctx->a);
ctx->a = a_tmp;
arr->data = a_tmp;
arr->capacity = number;
}
memcpy((char*)ctx->a + ctx->a_size * ctx->elemsize, elem,
ctx->elemsize * elements);
ctx->a_size += elements;
memcpy((char*)arr->data + arr->size * arr->elemsize, elems,
arr->elemsize * number);
arr->size += number;
return 1;
}
int array_Get(array_tCtx ctx, int idx, void** elem)
void* array_Copy(array_tCtx arr)
{
if (idx >= ctx->a_size)
return 0;
memcpy(elem, (char*)ctx->a + idx * ctx->elemsize, ctx->elemsize);
return 1;
}
int array_Size(array_tCtx ctx)
{
return ctx->a_size;
}
void* array_Array(array_tCtx ctx)
{
return ctx->a;
}
void* array_CopyArray(array_tCtx ctx)
{
void* buf;
buf = malloc(ctx->elemsize * ctx->a_size);
memcpy(buf, ctx->a, ctx->a_size * ctx->elemsize);
void* buf = malloc(arr->elemsize * arr->size);
memcpy(buf, arr->data, arr->size * arr->elemsize);
return buf;
}
......@@ -45,22 +45,47 @@ extern "C" {
#include <stddef.h>
#endif
/*! \file co_array.h
\brief Dynamic-size array, like std::vector from C++ or ArrayList from Java.
*/
typedef struct {
void* a;
void* data;
int elemsize;
int allocated;
int capacity;
int alloc_incr;
int a_size;
int size;
} array_sCtx, *array_tCtx;
int array_Init(array_tCtx* ctx, int elemsize, int alloc_incr);
void array_Close(array_tCtx ctx);
int array_Add(array_tCtx ctx, void* elem);
int array_MAdd(array_tCtx ctx, void* elem, int elements);
int array_Get(array_tCtx ctx, int idx, void** elem);
int array_Size(array_tCtx ctx);
void* array_Array(array_tCtx ctx);
void* array_CopyArray(array_tCtx ctx);
/*! \fn array_tCtx array_New(int elemsize, int alloc_incr)
\brief Allocates a new dynamic array containing elements of size \a elemsize
\param elemsize The sizeof() an element.
\param alloc_incr How much the dynamic array should grow when it is full.
\return Returns a new dynamic array.
*/
array_tCtx array_New(int elemsize, int alloc_incr);
/*! \fn void array_Delete(array_tCtx arr)
\brief Frees the memory held by \a arr
*/
void array_Delete(array_tCtx arr);
/*! \fn int array_Push(array_tCtx arr, void* elem)
\brief Appends \a elem to the end of the array.
*/
int array_Push(array_tCtx arr, void* elem);
/*! \fn int array_Push(array_tCtx arr, void* elem)
\brief Concatenates \a number of elements from the fixed-size array
\a elems to the end of the dynamic array \a arr.
*/
int array_Concat(array_tCtx arr, void* elems, int number);
/*! \fn void* array_Copy(array_tCtx arr)
\brief Creates and returns a copy of \a arr.
Warning! The returned array must be free() explicitly.
*/
void* array_Copy(array_tCtx arr);
#ifdef __cplusplus
}
......
......@@ -5153,21 +5153,20 @@ pwr_tStatus gdh_GetGlobalClassList(int cidcnt, pwr_tCid* cid, int attrobjects,
pwr_tStatus sts;
qcom_sNode mynode, node;
pwr_tNid nid;
array_tCtx arr;
pwr_tAttrRef* clist;
int ccnt;
int i;
pwr_tObjid oid;
pwr_tAttrRef aref;
array_Init(&arr, sizeof(pwr_tAttrRef), 20);
array_tCtx arr = array_New(sizeof(pwr_tAttrRef), 20);
/* Add local objects */
if (attrobjects) {
for (i = 0; i < cidcnt; i++) {
for (sts = gdh_GetClassListAttrRef(cid[i], &aref); ODD(sts);
sts = gdh_GetNextAttrRef(cid[i], &aref, &aref)) {
array_Add(arr, &aref);
array_Push(arr, &aref);
}
}
} else {
......@@ -5175,7 +5174,7 @@ pwr_tStatus gdh_GetGlobalClassList(int cidcnt, pwr_tCid* cid, int attrobjects,
for (sts = gdh_GetClassList(cid[i], &oid); ODD(sts);
sts = gdh_GetNextObject(oid, &oid)) {
aref = cdh_ObjidToAref(oid);
array_Add(arr, &aref);
array_Push(arr, &aref);
}
}
}
......@@ -5189,18 +5188,18 @@ pwr_tStatus gdh_GetGlobalClassList(int cidcnt, pwr_tCid* cid, int attrobjects,
if (EVEN(sts))
continue;
array_MAdd(arr, clist, ccnt);
array_Concat(arr, clist, ccnt);
}
if (array_Size(arr))
*classlist = array_CopyArray(arr);
if (arr->size)
*classlist = array_Copy(arr);
else {
*classlist = 0;
sts = GDH__NOSUCHOBJ;
}
*listcnt = array_Size(arr);
*listcnt = arr->size;
array_Close(arr);
array_Delete(arr);
return GDH__SUCCESS;
}
......@@ -5208,19 +5207,18 @@ pwr_tStatus gdh_GetLocalClassList(int cidcnt, pwr_tCid* cid, int attrobjects,
pwr_tAttrRef* classlist[], int* listcnt)
{
pwr_tStatus sts;
array_tCtx arr;
int i;
pwr_tObjid oid;
pwr_tAttrRef aref;
array_Init(&arr, sizeof(pwr_tAttrRef), 20);
array_tCtx arr = array_New(sizeof(pwr_tAttrRef), 20);
/* Add local objects */
if (attrobjects) {
for (i = 0; i < cidcnt; i++) {
for (sts = gdh_GetClassListAttrRef(cid[i], &aref); ODD(sts);
sts = gdh_GetNextAttrRef(cid[i], &aref, &aref)) {
array_Add(arr, &aref);
array_Push(arr, &aref);
}
}
} else {
......@@ -5228,20 +5226,20 @@ pwr_tStatus gdh_GetLocalClassList(int cidcnt, pwr_tCid* cid, int attrobjects,
for (sts = gdh_GetClassList(cid[i], &oid); ODD(sts);
sts = gdh_GetNextObject(oid, &oid)) {
aref = cdh_ObjidToAref(oid);
array_Add(arr, &aref);
array_Push(arr, &aref);
}
}
}
if (array_Size(arr))
*classlist = array_CopyArray(arr);
if (arr->size)
*classlist = array_Copy(arr);
else {
*classlist = 0;
sts = GDH__NOSUCHOBJ;
}
*listcnt = array_Size(arr);
*listcnt = arr->size;
array_Close(arr);
array_Delete(arr);
return GDH__SUCCESS;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment