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