1
0
mirror of https://github.com/TREX-CoE/qmckl.git synced 2024-06-30 00:44:52 +02:00
qmckl/src/qmckl_memory.org
2020-12-03 18:57:15 +01:00

2.5 KiB

Memory management

We override the allocation functions to enable the possibility of optimized libraries to fine-tune the memory allocation.

2 files are produced:

  • a source file : qmckl_memory.c
  • a test file : test_qmckl_memory.c

qmckl_malloc

Memory allocation function, letting the library choose how the memory will be allocated, and a pointer is returned to the user.

void* qmckl_malloc(const qmckl_context ctx, const size_t size);
interface
 type (c_ptr) function qmckl_malloc (context, size) bind(C)
   use, intrinsic :: iso_c_binding
   integer (c_int64_t), intent(in), value :: context
   integer (c_int64_t), intent(in), value :: size
 end function qmckl_malloc
end interface
Source
void* qmckl_malloc(const qmckl_context ctx, const size_t size) {
if (ctx == (qmckl_context) 0) {
/* Avoids unused parameter error */
return malloc( (size_t) size );
}
return malloc( (size_t) size );
}

qmckl_free

void qmckl_free(void *ptr);
interface
 subroutine qmckl_free (ptr) bind(C)
   use, intrinsic :: iso_c_binding
   type (c_ptr), intent(in), value :: ptr
 end subroutine qmckl_free
end interface
Source
void qmckl_free(void *ptr) {
free(ptr);
}