diff --git a/src/ivfflat.h b/src/ivfflat.h index 32b184a..13c3425 100644 --- a/src/ivfflat.h +++ b/src/ivfflat.h @@ -83,7 +83,7 @@ typedef struct VectorArrayData int length; int maxlen; int dim; - Vector items[FLEXIBLE_ARRAY_MEMBER]; + Vector *items; } VectorArrayData; typedef VectorArrayData * VectorArray; @@ -204,8 +204,8 @@ typedef struct IvfflatScanOpaqueData typedef IvfflatScanOpaqueData * IvfflatScanOpaque; -#define VECTOR_ARRAY_SIZE(_length, _dim) (offsetof(VectorArrayData, items) + _length * VECTOR_SIZE(_dim)) -#define VECTOR_ARRAY_OFFSET(_arr, _offset) ((char*) _arr + offsetof(VectorArrayData, items) + (_offset) * VECTOR_SIZE(_arr->dim)) +#define VECTOR_ARRAY_SIZE(_length, _dim) (sizeof(VectorArrayData) + (_length) * VECTOR_SIZE(_dim)) +#define VECTOR_ARRAY_OFFSET(_arr, _offset) ((char*) _arr->items + (_offset) * VECTOR_SIZE(_arr->dim)) #define VectorArrayGet(_arr, _offset) ((Vector *) VECTOR_ARRAY_OFFSET(_arr, _offset)) #define VectorArraySet(_arr, _offset, _val) (memcpy(VECTOR_ARRAY_OFFSET(_arr, _offset), _val, VECTOR_SIZE(_arr->dim))) diff --git a/src/ivfutils.c b/src/ivfutils.c index 2b5bb7d..b3caec8 100644 --- a/src/ivfutils.c +++ b/src/ivfutils.c @@ -10,11 +10,12 @@ VectorArray VectorArrayInit(int maxlen, int dimensions) { - VectorArray res = palloc_extended(VECTOR_ARRAY_SIZE(maxlen, dimensions), MCXT_ALLOC_ZERO | MCXT_ALLOC_HUGE); + VectorArray res = palloc(sizeof(VectorArrayData)); res->length = 0; res->maxlen = maxlen; res->dim = dimensions; + res->items = palloc_extended(maxlen * VECTOR_SIZE(dimensions), MCXT_ALLOC_ZERO | MCXT_ALLOC_HUGE); return res; } @@ -24,6 +25,7 @@ VectorArrayInit(int maxlen, int dimensions) void VectorArrayFree(VectorArray arr) { + pfree(arr->items); pfree(arr); }