paleolimbot commented on code in PR #14: URL: https://github.com/apache/arrow-nanoarrow/pull/14#discussion_r940252598
########## src/nanoarrow/nanoarrow.h: ########## @@ -503,6 +456,35 @@ static inline void ArrowBitmapReset(struct ArrowBitmap* bitmap); /// }@ +/// \defgroup nanoarrow-array Array producer helpers +/// These functions allocate, copy, and destroy ArrowArray structures + +/// \brief Initialize the fields of an array +/// +/// Initializes the fields and release callback of array. Caller +/// is responsible for calling the array->release callback if +/// NANOARROW_OK is returned. +ArrowErrorCode ArrowArrayInit(struct ArrowArray* array, enum ArrowType storage_type); + +/// \brief Allocate the array->children array +/// +/// Includes the memory for each child struct ArrowArray. +/// schema must have been allocated using ArrowArrayInit. +ArrowErrorCode ArrowArrayAllocateChildren(struct ArrowArray* array, int64_t n_children); + +/// \brief Allocate the array->dictionary member +/// +/// array must have been allocated using ArrowArrayInit Review Comment: They don't have to to avoid leaking memory (each member is marked released initially), but functionally that's probably what most people should do (or create the child arrays separately and move them). I added both those bits to the documentation. ########## src/nanoarrow/array.c: ########## @@ -0,0 +1,220 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "nanoarrow.h" + +static void ArrowArrayRelease(struct ArrowArray* array) { + // Release buffers held by this array + struct ArrowArrayPrivateData* data = (struct ArrowArrayPrivateData*)array->private_data; + if (data != NULL) { + ArrowBitmapReset(&data->bitmap); + ArrowBufferReset(&data->buffers[0]); + ArrowBufferReset(&data->buffers[1]); + ArrowFree(data); + } + + // This object owns the memory for all the children, but those + // children may have been generated elsewhere and might have + // their own release() callback. + if (array->children != NULL) { + for (int64_t i = 0; i < array->n_children; i++) { + if (array->children[i] != NULL) { + if (array->children[i]->release != NULL) { + array->children[i]->release(array->children[i]); + } + + ArrowFree(array->children[i]); + } + } + + ArrowFree(array->children); + } + + // This object owns the memory for the dictionary but it + // may have been generated somewhere else and have its own + // release() callback. + if (array->dictionary != NULL) { + if (array->dictionary->release != NULL) { + array->dictionary->release(array->dictionary); + } + + ArrowFree(array->dictionary); + } + + // Mark released + array->release = NULL; +} + +ArrowErrorCode ArrowArraySetStorageType(struct ArrowArray* array, + enum ArrowType storage_type) { + switch (storage_type) { + case NANOARROW_TYPE_UNINITIALIZED: + case NANOARROW_TYPE_NA: + array->n_buffers = 0; + break; + + case NANOARROW_TYPE_LIST: + case NANOARROW_TYPE_LARGE_LIST: + case NANOARROW_TYPE_STRUCT: + case NANOARROW_TYPE_MAP: + case NANOARROW_TYPE_SPARSE_UNION: Review Comment: Totally! (Done) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
