1
0
mirror of https://github.com/TREX-CoE/qmckl.git synced 2024-07-19 01:13:50 +02:00
qmckl/src/qmckl_memory.org
2020-10-25 15:16:02 +01:00

104 lines
3.2 KiB
Org Mode

# -*- mode: org -*-
# vim: syntax=c
#+TITLE: Memory management
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/>
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/>
#+HTML_HEAD: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
#+HTML_HEAD: <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/lib/js/jquery.stickytableheaders.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/readtheorg/js/readtheorg.js"></script>
We override the allocation functions to enable the possibility of
optimized libraries to fine-tune the memory allocation.
3 files are produced:
- a header file : =qmckl_memory.h=
- a source file : =qmckl_memory.c=
- a test file : =test_qmckl_memory.c=
** Header :noexport:
#+BEGIN_SRC C :comments link :tangle qmckl_memory.h
#ifndef QMCKL_MEMORY_H
#define QMCKL_MEMORY_H
#include "qmckl.h"
#+END_SRC
** Source :noexport:
#+BEGIN_SRC C :comments link :tangle qmckl_memory.c
#include <stdlib.h>
#include "qmckl_memory.h"
#+END_SRC
** Test :noexport:
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
#include "qmckl.h"
#include "munit.h"
MunitResult test_qmckl_memory() {
#+END_SRC
* =qmckl_malloc=
Analogous of =malloc, but passing a context and a signed 64-bit integers as argument.=
** Header
#+BEGIN_SRC C :comments link :tangle qmckl_memory.h
void* qmckl_malloc(const qmckl_context ctx, const size_t size);
#+END_SRC
** Source
#+BEGIN_SRC C :comments link :tangle qmckl_memory.c
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 );
}
#+END_SRC
** Test :noexport:
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
int *a;
a = (int*) qmckl_malloc( (qmckl_context) 1, 3*sizeof(int));
a[0] = 1;
a[1] = 2;
a[2] = 3;
munit_assert_int(a[0], ==, 1);
munit_assert_int(a[1], ==, 2);
munit_assert_int(a[2], ==, 3);
#+END_SRC
* =qmckl_free=
** Header
#+BEGIN_SRC C :comments link :tangle qmckl_memory.h
void qmckl_free(void *ptr);
#+END_SRC
** Source
#+BEGIN_SRC C :comments link :tangle qmckl_memory.c
void qmckl_free(void *ptr) {
free(ptr);
}
#+END_SRC
** Test :noexport:
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
qmckl_free(a);
#+END_SRC
* End of files :noexport:
** Header
#+BEGIN_SRC C :comments link :tangle qmckl_memory.h
#endif
#+END_SRC
** Test
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
return MUNIT_OK;
}
#+END_SRC