memory_manager.h (1391B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #ifndef LIB_JPEGLI_MEMORY_MANAGER_H_ 7 #define LIB_JPEGLI_MEMORY_MANAGER_H_ 8 9 #include <stdlib.h> 10 11 #include "lib/jpegli/common.h" 12 13 #define JPOOL_PERMANENT_ALIGNED (JPOOL_NUMPOOLS + JPOOL_PERMANENT) 14 #define JPOOL_IMAGE_ALIGNED (JPOOL_NUMPOOLS + JPOOL_IMAGE) 15 16 namespace jpegli { 17 18 void InitMemoryManager(j_common_ptr cinfo); 19 20 template <typename T> 21 T* Allocate(j_common_ptr cinfo, size_t len, int pool_id = JPOOL_PERMANENT) { 22 const size_t size = len * sizeof(T); // NOLINT 23 void* p = (*cinfo->mem->alloc_small)(cinfo, pool_id, size); 24 return reinterpret_cast<T*>(p); 25 } 26 27 template <typename T> 28 T* Allocate(j_decompress_ptr cinfo, size_t len, int pool_id = JPOOL_PERMANENT) { 29 return Allocate<T>(reinterpret_cast<j_common_ptr>(cinfo), len, pool_id); 30 } 31 32 template <typename T> 33 T* Allocate(j_compress_ptr cinfo, size_t len, int pool_id = JPOOL_PERMANENT) { 34 return Allocate<T>(reinterpret_cast<j_common_ptr>(cinfo), len, pool_id); 35 } 36 37 template <typename T> 38 JBLOCKARRAY GetBlockRow(T cinfo, int c, JDIMENSION by) { 39 return (*cinfo->mem->access_virt_barray)( 40 reinterpret_cast<j_common_ptr>(cinfo), cinfo->master->coeff_buffers[c], 41 by, 1, true); 42 } 43 44 } // namespace jpegli 45 46 #endif // LIB_JPEGLI_MEMORY_MANAGER_H_