Program Listing for File Allocator.h

Return to documentation for file (nvcv_types/include/nvcv/alloc/Allocator.h)

/*
 * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed 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.
 */
#ifndef NVCV_ALLOCATOR_H
#define NVCV_ALLOCATOR_H

#include "../Export.h"
#include "../Status.h"
#include "Fwd.h"

#include <stdalign.h>

#ifdef __cplusplus
extern "C"
{
#endif

typedef void *(*NVCVMemAllocFunc)(void *ctx, int64_t sizeBytes, int32_t alignBytes);

typedef void (*NVCVMemFreeFunc)(void *ctx, void *ptr, int64_t sizeBytes, int32_t alignBytes);

typedef enum
{
    NVCV_RESOURCE_MEM_HOST,
    NVCV_RESOURCE_MEM_CUDA,
    NVCV_RESOURCE_MEM_HOST_PINNED
} NVCVResourceType;

#define NVCV_NUM_RESOURCE_TYPES (3)

typedef struct NVCVCustomMemAllocatorRec
{
    NVCVMemAllocFunc fnAlloc;

    NVCVMemFreeFunc fnFree;
} NVCVCustomMemAllocator;

typedef union NVCVCustomResourceAllocatorRec
{
    NVCVCustomMemAllocator mem;
} NVCVCustomResourceAllocator;

typedef struct NVCVResourceAllocatorRec NVCVResourceAllocator;

typedef void (*NVCVResourceAllocatorCleanupFunc)(void *ctx, NVCVResourceAllocator *data);

struct NVCVResourceAllocatorRec
{
    void *ctx;

    NVCVResourceType resType;

    NVCVCustomResourceAllocator res;

    NVCVResourceAllocatorCleanupFunc cleanup;
};

typedef struct NVCVAllocator *NVCVAllocatorHandle;

NVCV_PUBLIC NVCVStatus nvcvAllocatorConstructCustom(const NVCVResourceAllocator *customAllocators,
                                                    int32_t numCustomAllocators, NVCVAllocatorHandle *handle);

NVCV_PUBLIC NVCVStatus nvcvAllocatorDecRef(NVCVAllocatorHandle handle, int *newRefCount);

NVCV_PUBLIC NVCVStatus nvcvAllocatorIncRef(NVCVAllocatorHandle handle, int *newRefCount);

NVCV_PUBLIC NVCVStatus nvcvAllocatorRefCount(NVCVAllocatorHandle handle, int *newRefCount);

NVCV_PUBLIC NVCVStatus nvcvAllocatorGet(NVCVAllocatorHandle handle, NVCVResourceType resType,
                                        NVCVResourceAllocator *result);

NVCV_PUBLIC NVCVStatus nvcvAllocatorSetUserPointer(NVCVAllocatorHandle handle, void *userPtr);

NVCV_PUBLIC NVCVStatus nvcvAllocatorGetUserPointer(NVCVAllocatorHandle handle, void **outUserPtr);

NVCV_PUBLIC NVCVStatus nvcvAllocatorAllocHostMemory(NVCVAllocatorHandle halloc, void **ptr, int64_t sizeBytes,
                                                    int32_t alignBytes);

NVCV_PUBLIC NVCVStatus nvcvAllocatorFreeHostMemory(NVCVAllocatorHandle halloc, void *ptr, int64_t sizeBytes,
                                                   int32_t alignBytes);

NVCV_PUBLIC NVCVStatus nvcvAllocatorAllocHostPinnedMemory(NVCVAllocatorHandle halloc, void **ptr, int64_t sizeBytes,
                                                          int32_t alignBytes);

NVCV_PUBLIC NVCVStatus nvcvAllocatorFreeHostPinnedMemory(NVCVAllocatorHandle halloc, void *ptr, int64_t sizeBytes,
                                                         int32_t alignBytes);

NVCV_PUBLIC NVCVStatus nvcvAllocatorAllocCudaMemory(NVCVAllocatorHandle halloc, void **ptr, int64_t sizeBytes,
                                                    int32_t alignBytes);

NVCV_PUBLIC NVCVStatus nvcvAllocatorFreeCudaMemory(NVCVAllocatorHandle halloc, void *ptr, int64_t sizeBytes,
                                                   int32_t alignBytes);

NVCV_PUBLIC const char *nvcvResourceTypeGetName(NVCVResourceType resource);

#ifdef __cplusplus
}
#endif

#endif // NVCV_ALLOCATOR_H