1
0
mirror of https://github.com/TREX-CoE/qmckl.git synced 2024-11-03 12:43:57 +01:00
qmckl/org/qmckl_memory.org

290 lines
7.9 KiB
Org Mode
Raw Normal View History

2021-03-09 01:16:23 +01:00
#+TITLE: Memory management
2021-04-30 01:26:19 +02:00
#+SETUPFILE: ../tools/theme.setup
#+INCLUDE: ../tools/lib.org
2020-10-22 01:24:14 +02:00
2021-03-09 01:16:23 +01:00
We override the allocation functions to enable the possibility of
optimized libraries to fine-tune the memory allocation.
2020-10-16 23:56:22 +02:00
2021-03-09 01:16:23 +01:00
* Headers :noexport:
#+begin_src c :tangle (eval c)
2021-05-10 10:05:50 +02:00
#ifdef HAVE_CONFIG_H
2021-05-10 10:41:59 +02:00
#include "config.h"
2021-05-09 02:12:38 +02:00
#endif
2021-05-10 10:05:50 +02:00
#ifdef HAVE_STDINT_H
2021-03-09 01:16:23 +01:00
#include <stdint.h>
2021-05-10 10:05:50 +02:00
#elif HAVE_INTTYPES_H
#include <inttypes.h>
#endif
2021-03-09 01:16:23 +01:00
#include <stdlib.h>
2021-03-30 22:40:56 +02:00
#include <string.h>
2021-03-10 12:58:38 +01:00
#include <assert.h>
2021-05-11 13:57:23 +02:00
#include "qmckl.h"
2021-03-30 22:40:56 +02:00
#include "qmckl_memory_private_type.h"
2021-03-30 14:51:23 +02:00
#include "qmckl_context_private_type.h"
2021-03-30 22:40:56 +02:00
#include "qmckl_memory_private_func.h"
2021-03-09 01:16:23 +01:00
#+end_src
#+begin_src c :tangle (eval c_test) :noweb yes
2020-10-17 01:10:54 +02:00
#include "qmckl.h"
2021-05-11 16:41:03 +02:00
#include "assert.h"
2021-05-10 10:05:50 +02:00
#ifdef HAVE_CONFIG_H
2021-05-10 10:41:59 +02:00
#include "config.h"
2021-05-09 02:12:38 +02:00
#endif
2021-03-30 22:40:56 +02:00
#include "qmckl_context_private_type.h"
#include "qmckl_memory_private_func.h"
2021-05-11 16:41:03 +02:00
int main() {
2021-03-09 01:16:23 +01:00
#+end_src
2020-10-16 23:56:22 +02:00
2021-03-30 22:40:56 +02:00
#+begin_src c :tangle (eval h_private_type) :noweb yes
#ifndef QMCKL_MEMORY_HPT
#define QMCKL_MEMORY_HPT
#include <stdint.h>
2021-05-12 23:51:59 +02:00
#include <stdlib.h>
2021-03-30 22:40:56 +02:00
#+end_src
* Memory data structure for the context
2021-04-30 01:26:19 +02:00
2021-03-30 22:40:56 +02:00
Every time a new block of memory is allocated, the information
relative to the allocation is stored in a new ~qmckl_memory_info_struct~.
2021-04-30 01:26:19 +02:00
A ~qmckl_memory_info_struct~ contains the pointer to the memory block,
2021-03-30 22:40:56 +02:00
its size in bytes, and extra implementation-specific information such as
alignment, pinning, if the memory should be allocated on CPU or GPU
/etc/.
#+begin_src c :tangle (eval h_private_type) :noweb yes
typedef struct qmckl_memory_info_struct {
size_t size;
void* pointer;
} qmckl_memory_info_struct;
static const qmckl_memory_info_struct qmckl_memory_info_struct_zero =
{
.size = (size_t) 0,
.pointer = NULL
};
#+end_src
2021-04-30 01:26:19 +02:00
2021-03-30 22:40:56 +02:00
The ~memory~ element of the context is a data structure which
contains an array of ~qmckl_memory_info_struct~, the size of the
array, and the number of allocated blocks.
2021-04-30 01:26:19 +02:00
2021-03-30 22:40:56 +02:00
#+begin_src c :tangle (eval h_private_type) :noweb yes
typedef struct qmckl_memory_struct {
size_t n_allocated;
size_t array_size;
qmckl_memory_info_struct* element;
} qmckl_memory_struct;
#+end_src
* Passing info to allocation routines
Passing information to the allocation routine should be done by
passing an instance of a ~qmckl_memory_info_struct~.
2021-04-30 01:26:19 +02:00
2021-03-30 22:40:56 +02:00
* Allocation/deallocation functions
2021-03-18 23:55:50 +01:00
2021-03-10 12:58:38 +01:00
Memory allocation inside the library should be done with
~qmckl_malloc~. It lets the library choose how the memory will be
allocated, and a pointer is returned to the user. The context is
passed to let the library store data related to the allocation
inside the context. In this particular implementation of the library,
we store a list of allocated pointers so that all the memory can be
properly freed when the library is de-initialized.
If the allocation failed, the ~NULL~ pointer is returned.
# Header
2021-03-30 22:40:56 +02:00
#+begin_src c :tangle (eval h_private_func) :noexport
2021-03-10 12:58:38 +01:00
void* qmckl_malloc(qmckl_context context,
2021-03-30 22:40:56 +02:00
const qmckl_memory_info_struct info);
2021-03-09 01:16:23 +01:00
#+end_src
2021-04-30 01:26:19 +02:00
2021-03-10 12:58:38 +01:00
# Source
#+begin_src c :tangle (eval c)
2021-03-30 22:40:56 +02:00
void* qmckl_malloc(qmckl_context context, const qmckl_memory_info_struct info) {
2020-10-16 23:56:22 +02:00
2021-03-30 14:51:23 +02:00
assert (qmckl_context_check(context) != QMCKL_NULL_CONTEXT);
2021-03-10 12:58:38 +01:00
2021-03-30 22:40:56 +02:00
qmckl_context_struct* const ctx = (qmckl_context_struct* const) context;
/* Allocate memory and zero it */
void * pointer = malloc(info.size);
if (pointer == NULL) {
return NULL;
2021-03-10 12:58:38 +01:00
}
2021-03-30 22:40:56 +02:00
memset(pointer, 0, info.size);
qmckl_lock(context);
{
/* If qmckl_memory_struct is full, reallocate a larger one */
if (ctx->memory.n_allocated == ctx->memory.array_size) {
const size_t old_size = ctx->memory.array_size;
2021-05-12 23:57:40 +02:00
qmckl_memory_info_struct * new_array = realloc(ctx->memory.element,
2L * old_size *
2021-03-30 22:40:56 +02:00
sizeof(qmckl_memory_info_struct));
if (new_array == NULL) {
qmckl_unlock(context);
2021-03-31 01:52:43 +02:00
free(pointer);
2021-03-30 22:40:56 +02:00
return NULL;
}
2021-04-30 01:26:19 +02:00
2021-03-30 22:40:56 +02:00
memset( &(new_array[old_size]), 0, old_size * sizeof(qmckl_memory_info_struct) );
ctx->memory.element = new_array;
ctx->memory.array_size = 2L * old_size;
}
2021-04-30 01:26:19 +02:00
2021-03-30 22:40:56 +02:00
/* Find first NULL entry */
size_t pos = (size_t) 0;
while ( pos < ctx->memory.array_size && ctx->memory.element[pos].size > (size_t) 0) {
pos += (size_t) 1;
}
assert (ctx->memory.element[pos].size == (size_t) 0);
2021-03-10 12:58:38 +01:00
2021-03-30 22:40:56 +02:00
/* Copy info at the new location */
ctx->memory.element[pos].size = info.size;
ctx->memory.element[pos].pointer = pointer;
ctx->memory.n_allocated += (size_t) 1;
}
qmckl_unlock(context);
2021-04-30 01:26:19 +02:00
2021-03-10 12:58:38 +01:00
return pointer;
}
#+end_src
2021-03-18 23:55:50 +01:00
2021-03-09 01:16:23 +01:00
2021-03-18 23:55:50 +01:00
# Test :noexport:
2021-03-09 01:16:23 +01:00
#+begin_src c :tangle (eval c_test)
2021-03-30 22:40:56 +02:00
/* Create a context */
2021-04-30 01:26:19 +02:00
qmckl_context context = qmckl_context_create();
2021-03-09 01:16:23 +01:00
2021-03-30 22:40:56 +02:00
qmckl_memory_info_struct info = qmckl_memory_info_struct_zero;
info.size = (size_t) 3;
2021-03-18 23:55:50 +01:00
2021-03-30 22:40:56 +02:00
/* Allocate an array of ints */
int *a = (int*) qmckl_malloc(context, info);
/* Check that array of ints is OK */
2021-05-11 16:41:03 +02:00
assert(a != NULL);
a[0] = 1; assert(a[0] == 1);
a[1] = 2; assert(a[1] == 2);
a[2] = 3; assert(a[2] == 3);
2021-03-30 22:40:56 +02:00
/* Allocate another array of ints */
int *b = (int*) qmckl_malloc(context, info);
/* Check that array of ints is OK */
2021-05-11 16:41:03 +02:00
assert(b != NULL);
b[0] = 1; assert(b[0] == 1);
b[1] = 2; assert(b[1] == 2);
b[2] = 3; assert(b[2] == 3);
2021-03-18 23:55:50 +01:00
#+end_src
2021-03-09 01:16:23 +01:00
2021-03-18 23:55:50 +01:00
When freeing the memory with ~qmckl_free~, the context is passed, in
case some important information has been stored related to memory
allocation and needs to be updated.
2021-03-09 01:16:23 +01:00
2021-03-30 14:51:23 +02:00
#+begin_src c :tangle (eval h_func)
2021-03-09 01:16:23 +01:00
qmckl_exit_code qmckl_free(qmckl_context context,
2021-03-30 22:40:56 +02:00
void * const ptr);
2021-03-09 01:16:23 +01:00
#+end_src
2021-03-05 03:45:30 +01:00
2021-03-18 23:55:50 +01:00
# Source
2021-03-09 01:16:23 +01:00
#+begin_src c :tangle (eval c)
2021-03-30 22:40:56 +02:00
qmckl_exit_code qmckl_free(qmckl_context context, void * const ptr) {
2021-03-10 12:58:38 +01:00
2021-03-30 22:40:56 +02:00
if (qmckl_context_check(context) == QMCKL_NULL_CONTEXT) {
2021-03-10 12:58:38 +01:00
return qmckl_failwith(context,
2021-03-30 22:40:56 +02:00
QMCKL_INVALID_CONTEXT,
2021-03-10 12:58:38 +01:00
"qmckl_free",
2021-03-30 22:40:56 +02:00
NULL);
}
2021-04-30 01:26:19 +02:00
2021-03-30 22:40:56 +02:00
if (ptr == NULL) {
return qmckl_failwith(context,
QMCKL_INVALID_ARG_2,
"qmckl_free",
"NULL pointer");
}
2021-04-30 01:26:19 +02:00
2021-03-30 22:40:56 +02:00
qmckl_context_struct* const ctx = (qmckl_context_struct* const) context;
qmckl_lock(context);
{
/* Find pointer in array of saved pointers */
size_t pos = (size_t) 0;
while ( pos < ctx->memory.array_size && ctx->memory.element[pos].pointer != ptr) {
pos += (size_t) 1;
2021-03-10 12:58:38 +01:00
}
2021-04-30 01:26:19 +02:00
2021-03-30 22:40:56 +02:00
if (pos >= ctx->memory.array_size) {
/* Not found */
qmckl_unlock(context);
return qmckl_failwith(context,
QMCKL_FAILURE,
"qmckl_free",
"Pointer not found in context");
}
free(ptr);
2021-03-10 12:58:38 +01:00
2021-03-30 22:40:56 +02:00
memset( &(ctx->memory.element[pos]), 0, sizeof(qmckl_memory_info_struct) );
ctx->memory.n_allocated -= (size_t) 1;
2021-03-10 12:58:38 +01:00
}
2021-03-30 22:40:56 +02:00
qmckl_unlock(context);
2021-03-05 03:45:30 +01:00
return QMCKL_SUCCESS;
2020-10-16 23:56:22 +02:00
}
2021-03-09 01:16:23 +01:00
#+end_src
2020-10-22 01:24:14 +02:00
2021-03-18 23:55:50 +01:00
# Test
#+begin_src c :tangle (eval c_test) :exports none
2021-03-05 03:45:30 +01:00
qmckl_exit_code rc;
2021-03-30 22:40:56 +02:00
/* Assert that both arrays are allocated */
2021-05-11 16:41:03 +02:00
assert(a != NULL);
assert(b != NULL);
2021-03-30 22:40:56 +02:00
/* Free in NULL context */
rc = qmckl_free(QMCKL_NULL_CONTEXT, a);
2021-05-11 16:41:03 +02:00
assert(rc == QMCKL_INVALID_CONTEXT);
2021-03-30 22:40:56 +02:00
/* Free NULL pointer */
rc = qmckl_free(context, NULL);
2021-05-11 16:41:03 +02:00
assert(rc == QMCKL_INVALID_ARG_2);
2021-03-30 22:40:56 +02:00
/* Free for the first time */
2021-03-18 23:55:50 +01:00
rc = qmckl_free(context, a);
2021-05-11 16:41:03 +02:00
assert(rc == QMCKL_SUCCESS);
2021-03-18 23:55:50 +01:00
2021-03-30 22:40:56 +02:00
/* Free again */
rc = qmckl_free(context, a);
2021-05-11 16:41:03 +02:00
assert(rc == QMCKL_FAILURE);
2021-03-30 22:40:56 +02:00
/* Clean up */
2021-03-18 23:55:50 +01:00
rc = qmckl_context_destroy(context);
2021-05-11 16:41:03 +02:00
assert(rc == QMCKL_SUCCESS);
2021-02-19 01:39:42 +01:00
2021-03-09 01:16:23 +01:00
#+end_src
2020-10-16 23:56:22 +02:00
2021-03-09 01:16:23 +01:00
* End of files :noexport:
2020-10-16 23:56:22 +02:00
2021-03-30 22:40:56 +02:00
#+begin_src c :comments org :tangle (eval h_private_type)
#endif
#+end_src
2021-03-09 01:16:23 +01:00
** Test
#+begin_src c :comments org :tangle (eval c_test)
2021-05-11 16:41:03 +02:00
return 0;
2020-10-22 01:24:14 +02:00
}
2020-10-17 01:10:54 +02:00
2021-03-09 01:16:23 +01:00
#+end_src
2021-04-30 01:26:19 +02:00
2020-11-05 15:27:25 +01:00
2021-03-09 01:16:23 +01:00
# -*- mode: org -*-
# vim: syntax=c