# Memory management

- FASTCV\_API int fcvPyramidAllocate([fcvPyramidLevel](https://docs.qualcomm.com/doc/80-79511-2/topic/struct_a00017.html#_CPPv415fcvPyramidLevel) \*pyr, unsigned int baseWidth, unsigned int baseHeight, unsigned int bytesPerPixel, unsigned int numLevels, int allocateBase)

    - Allocates memory for Pyramid.

**ATTENTION:** This function’s signature will become **OBSOLETE** in a future release of this library (2.0.0). The new interface is specified in the function: [fcvPyramidAllocate\_v2()](https://docs.qualcomm.com/doc/80-79511-2/topic/memory_management.html#a00032_1ga98ab29f786ca1361d7dca374a8fedb24). In the 2.0.0 release, fcvPyramidAllocate\_v2 will be renamed to fcvPyramidAllocate and the signature of fcvPyramidAllocate as it appears now, will be removed.

- Parameters:

    - - **pyr** – Pointer to an array of [fcvPyramidLevel](https://docs.qualcomm.com/doc/80-79511-2/topic/struct_a00017.html#a00017)
- **baseWidth** – Width of the base level: the value assigned to pyr[0].width
- **baseHeight** – Height of the base level: the value assigned to pyr[0].height
- **bytesPerPixel** –

    Number of bytes per pixel:

    e.g for uint8\_t pyramid, bytesPerPixel = 1

 for int32\_t pyramid, bytesPerPixel = 4
- **numLevels** – number of levels in the pyramid
- **allocateBase** –

    if set to 1, memory will be allocated for the base level

    if set to 0, memory for the base level is allocated by the callee

**WARNING:** How this parameter is set will impact how the memory is freed. Please refer to fcvPyramidDelete for details.

- FASTCV\_API int fcvPyramidAllocate\_v2([fcvPyramidLevel\_v2](https://docs.qualcomm.com/doc/80-79511-2/topic/struct_a00018.html#_CPPv418fcvPyramidLevel_v2) \*pyr, uint32\_t baseWidth, uint32\_t baseHeight, uint32\_t baseStride, uint32\_t bytesPerPixel, uint32\_t numLevels, int32\_t allocateBase)

    - Allocates memory for Pyramid.

**ATTENTION:** This function is a duplication of [fcvPyramidAllocate()](https://docs.qualcomm.com/doc/80-79511-2/topic/memory_management.html#a00032_1ga055f303bce6edd36be8201360b6c49ae) with the addition of extra parameters. This function has been added to allow for backward compatibility with the original function. When the 2.0.0 release of this library is made, this function will be renamed to: *fcvPyramidAllocate*, *fcvPyramidAllocate\_v2* will be removed, and the current signature for *fcvPyramidAllocate* will be removed. Until 2.0.0, the developer should use this implementation with the expectation of renaming it to *fcvPyramidAllocate* when transitioning to 2.0.0.

- Parameters:

    - - **pyr** – Pointer to an array of [fcvPyramidLevel\_v2](https://docs.qualcomm.com/doc/80-79511-2/topic/struct_a00018.html#a00018)
- **baseWidth** – Width of the base level: the value assigned to pyr[0].width
- **baseHeight** – Height of the base level: the value assigned to pyr[0].height
- **baseStride** – Stride of image is the number of bytes between column 0 of row 1 and column 0 of row 2 in data memory. Stride of the base level: the value assigned to pyr[0].stride **NOTE:** stride of non-base pyramid image is the same as width\*bytesPerPixel of the non-base pyramid image
- **bytesPerPixel** –

    Number of bytes per pixel:

    e.g for uint8\_t pyramid, bytesPerPixel = 1

 for int32\_t pyramid, bytesPerPixel = 4
- **numLevels** – number of levels in the pyramid
- **allocateBase** –

    if set to 1, memory will be allocated for the base level

    if set to 0, memory for the base level is allocated by the callee

**WARNING:** How this parameter is set will impact how the memory is freed. Please refer to fcvPyramidDelete for details.

- FASTCV\_API [fcvStatus](https://docs.qualcomm.com/doc/80-79511-2/topic/enum_a00021_1a39574810b4ad6914ee8613424f316283.html#_CPPv49fcvStatus) fcvPyramidAllocate\_v3([fcvPyramidLevel\_v2](https://docs.qualcomm.com/doc/80-79511-2/topic/struct_a00018.html#_CPPv418fcvPyramidLevel_v2) \*\_\_restrict pyr, uint32\_t baseWidth, uint32\_t baseHeight, uint32\_t baseStride, uint32\_t bytesPerPixel, uint32\_t alignment, uint32\_t numLevels, [fcvPyramidScale](https://docs.qualcomm.com/doc/80-79511-2/topic/enum_a00021_1a753b43f34bc209915be1aa6a00d337ab.html#_CPPv415fcvPyramidScale) scale, int32\_t allocateBase)

    - Allocates memory for Pyramid DO NOT USE THIS API unless for testing purposes. This API can be removed without notice.

**ATTENTION:** This function is a duplication of [fcvPyramidAllocate\_v2()](https://docs.qualcomm.com/doc/80-79511-2/topic/memory_management.html#a00032_1ga98ab29f786ca1361d7dca374a8fedb24) with the addition of extra parameters. This function has been added to allow for backward compatibility with the original function. When the 2.0.0 release of this library is made, this function will be renamed to: *fcvPyramidAllocate*, *fcvPyramidAllocate\_v3* will be removed, and the current signature for *fcvPyramidAllocate* will be removed. Until 2.0.0, the developer should use this implementation with the expectation of renaming it to *fcvPyramidAllocate* when transitioning to 2.0.0.

- Parameters:

    - - **pyr** – Pointer to an array of [fcvPyramidLevel\_v2](https://docs.qualcomm.com/doc/80-79511-2/topic/struct_a00018.html#a00018)
- **baseWidth** – Width of the base level: the value assigned to pyr[0].width
- **baseHeight** – Height of the base level: the value assigned to pyr[0].height
- **baseStride** – Stride of image is the number of bytes between column 0 of row 1 and column 0 of row 2 in data memory. Stride of the base level: the value assigned to pyr[0].stride **NOTE:** stride of non-base pyramid image is the same as width\*bytesPerPixel of the non-base pyramid image
- **bytesPerPixel** –

    Number of bytes per pixel:

    e.g for uint8\_t pyramid, bytesPerPixel = 1

 for int32\_t pyramid, bytesPerPixel = 4
- **alignment** – Used to specify the alignment of each pyramid level other than the base. Must be a multiple of 8. If set to 0, the alignment will be set as 8 bits.
- **numLevels** – number of levels in the pyramid
- **scale** – Defines the type of scaling used for each pyramid level. FASTCV\_PYRAMID\_SCALE\_HALF downscales each level of the pyramid by a factor of 2. FASTCV\_PYRAMID\_SCALE\_ORB downscales each level of the pyramid by a factor of 1/(2)^(1/4), which is approximated as 0.8408964f
- **allocateBase** –

    if set to 1, memory will be allocated for the base level

    if set to 0, memory for the base level is allocated by the callee

**WARNING:** How this parameter is set will impact how the memory is freed. Please refer to fcvPyramidDelete\_v2 for details.

- Returns:

    - FASTCV\_SUCCESS upon success. Other status codes upon failure.

- FASTCV\_API void fcvPyramidDelete([fcvPyramidLevel](https://docs.qualcomm.com/doc/80-79511-2/topic/struct_a00017.html#_CPPv415fcvPyramidLevel) \*pyr, unsigned int numLevels, unsigned int startLevel)

    - Deallocates an array of [fcvPyramidLevel](https://docs.qualcomm.com/doc/80-79511-2/topic/struct_a00017.html#a00017). Can be used for any type(f32/s8/u8).

**ATTENTION:** This function’s signature will become **OBSOLETE** in a future release of this library (2.0.0). The new interface is specified in the function: [fcvPyramidDelete\_v2()](https://docs.qualcomm.com/doc/80-79511-2/topic/memory_management.html#a00032_1ga953c655f3d22e1e9c739838d2adb7a9e). In the 2.0.0 release, fcvPyramidDelete\_v2 will be renamed to fcvPyramidDelete and the signature of fcvPyramidDelete as it appears now, will be removed.

- Parameters:

    - - **pyr** – pyramid to deallocate
- **numLevels** – Number of levels in the pyramid
- **startLevel** – Start level of the pyramid **WARNING:** if pyr was allocated with allocateBase=0 which means baselevel memory was allocated outside of fcvPyramidAllocate, then startLevel for fcvPyramidDelete has to be set to 1 (or higher).

- FASTCV\_API void fcvPyramidDelete\_v2([fcvPyramidLevel\_v2](https://docs.qualcomm.com/doc/80-79511-2/topic/struct_a00018.html#_CPPv418fcvPyramidLevel_v2) \*pyr, uint32\_t numLevels, uint32\_t startLevel)

    - Deallocates an array of [fcvPyramidLevel](https://docs.qualcomm.com/doc/80-79511-2/topic/struct_a00017.html#a00017). Can be used for any type(f32/s8/u8).

**ATTENTION:** This function is a duplication of [fcvPyramidDelete()](https://docs.qualcomm.com/doc/80-79511-2/topic/memory_management.html#a00032_1ga8956692c45c227572c0431b2a58e6157) with the addition of extra parameters. This function has been added to allow for backward compatibility with the original function. When the 2.0.0 release of this library is made, this function will be renamed to: *fcvPyramidDelete*, *fcvPyramidDelete\_v2* will be removed, and the current signature for *fcvPyramidDelete* will be removed. Until 2.0.0, the developer should use this implementation with the expectation of renaming it to *fcvPyramidDelete* when transitioning to 2.0.0.

- Parameters:

    - - **pyr** – pyramid to deallocate
- **numLevels** – Number of levels in the pyramid
- **startLevel** – Start level of the pyramid **WARNING:** if pyr was allocated with allocateBase=0 which means baselevel memory was allocated outside of fcvPyramidAllocate, then startLevel for fcvPyramidDelete\_v2 has to be set to 1 (or higher).

- FASTCV\_API void \*fcvMemAlloc(unsigned int nBytes, unsigned int byteAlignment)

    - Allocates aligned memory.

- Parameters:

    - - **nBytes** – Number of bytes.
- **byteAlignment** – Alignment specified in bytes (e.g., 16 = 128-bit alignment). **WARNING:** must be &lt; 255 bytes

- Returns:

    - SUCCESS: pointer to aligned memory FAILURE: 0

- FASTCV\_API void fcvMemFree(void \*ptr)

    - Frees memory allocated by [fcvMemAlloc()](https://docs.qualcomm.com/doc/80-79511-2/topic/memory_management.html#a00032_1ga7dd35fabc0d9fc95ea5746b920b15988).

- Parameters:

    - **ptr** – Pointer to memory.

- FASTCV\_API void fcvMemInit(void)

    - Initialize the Memory sub-system in FastCV. The sub-system handles temporary/scratch memory requirements from several FastCV functions that do not have parameters to have this supplied by the users of those functions.

**Important:** 
 1. Every call to this function should be followed by a call to [fcvMemDeInit()](https://docs.qualcomm.com/doc/80-79511-2/topic/memory_management.html#a00032_1ga814f9ce5b8a92f677f0485497b6c8869)

.

2. This function (and the fcvMemDeInit) only needs to called once per process.

3. Any call to FastCV functions that uses internal temporary memory outside of fcvMemInit and fcvMemDeInit pair will use non-optimum memory allocation.

4. A call to this function without a corresponding call to fcvMemDeInit can result in memory leak.

- FASTCV\_API void fcvMemInitPreAlloc(uint32\_t preAllocBytes)

    - Initialize the Memory sub-system in FastCV with pre-allocated buffer. The sub-system handles temporary/scratch memory requirements from several FastCV functions that do not have parameters to have this supplied by the users of those functions.

**Important:** 
 1. Every call to this function should be followed by a call to [fcvMemDeInit()](https://docs.qualcomm.com/doc/80-79511-2/topic/memory_management.html#a00032_1ga814f9ce5b8a92f677f0485497b6c8869)

.

2. This function (and the fcvMemDeInit) only needs to called once per process.

3. Any call to FastCV functions that uses internal temporary memory outside of fcvMemInit and fcvMemDeInit pair will use non-optimum memory allocation.

4. A call to this function without a corresponding call to fcvMemDeInit can result in memory leak.

- Parameters:

    - **preAllocBytes** – Number of bytes for the pre-allocated buffer.

- FASTCV\_API void fcvMemDeInit(void)

    - De-Initialize the Memory sub-system initialized by [fcvMemInit()](https://docs.qualcomm.com/doc/80-79511-2/topic/memory_management.html#a00032_1ga421455e5b13eda9a42c0d660ecb697c6) function.

**Important:** 
 1. Call to this function without a corresponding call to fcvMemInit will result in no-op.

Last Published: Aug 30, 2024

[Previous Topic
Mathvector operations](https://docs.qualcomm.com/bundle/publicresource/80-79511-2/topics/mathvector_operations.md) [Next Topic
Miscellaneous](https://docs.qualcomm.com/bundle/publicresource/80-79511-2/topics/miscellaneous.md)