2020-10-16 23:56:22 +02:00
|
|
|
# -*- mode: org -*-
|
|
|
|
# vim: syntax=c
|
|
|
|
#+TITLE: Memory management
|
|
|
|
|
2020-10-22 01:24:14 +02:00
|
|
|
#+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>
|
|
|
|
|
|
|
|
|
2020-10-16 23:56:22 +02:00
|
|
|
We override the allocation functions to enable the possibility of
|
|
|
|
optimized libraries to fine-tune the memory allocation.
|
|
|
|
|
|
|
|
3 files are produced:
|
2020-10-22 01:24:14 +02:00
|
|
|
- a header file : =qmckl_memory.h=
|
|
|
|
- a source file : =qmckl_memory.c=
|
|
|
|
- a test file : =test_qmckl_memory.c=
|
2020-10-16 23:56:22 +02:00
|
|
|
|
2020-10-25 15:16:02 +01:00
|
|
|
** Header :noexport:
|
2020-10-22 01:24:14 +02:00
|
|
|
#+BEGIN_SRC C :comments link :tangle qmckl_memory.h
|
2020-10-16 23:56:22 +02:00
|
|
|
#ifndef QMCKL_MEMORY_H
|
|
|
|
#define QMCKL_MEMORY_H
|
|
|
|
#include "qmckl.h"
|
2020-10-22 01:24:14 +02:00
|
|
|
#+END_SRC
|
2020-10-16 23:56:22 +02:00
|
|
|
|
2020-10-25 15:16:02 +01:00
|
|
|
** Source :noexport:
|
2020-10-22 01:24:14 +02:00
|
|
|
#+BEGIN_SRC C :comments link :tangle qmckl_memory.c
|
2020-10-16 23:56:22 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "qmckl_memory.h"
|
2020-10-22 01:24:14 +02:00
|
|
|
#+END_SRC
|
2020-10-16 23:56:22 +02:00
|
|
|
|
2020-10-25 15:16:02 +01:00
|
|
|
** Test :noexport:
|
2020-10-22 01:24:14 +02:00
|
|
|
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
|
2020-10-17 01:10:54 +02:00
|
|
|
#include "qmckl.h"
|
|
|
|
#include "munit.h"
|
2020-10-21 19:50:18 +02:00
|
|
|
MunitResult test_qmckl_memory() {
|
2020-10-22 01:24:14 +02:00
|
|
|
#+END_SRC
|
2020-10-16 23:56:22 +02:00
|
|
|
|
2020-10-22 01:24:14 +02:00
|
|
|
* =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
|
2020-10-22 00:50:07 +02:00
|
|
|
void* qmckl_malloc(const qmckl_context ctx, const size_t size);
|
2020-10-22 01:24:14 +02:00
|
|
|
#+END_SRC
|
2020-10-16 23:56:22 +02:00
|
|
|
|
2020-10-22 01:24:14 +02:00
|
|
|
** Source
|
|
|
|
#+BEGIN_SRC C :comments link :tangle qmckl_memory.c
|
2020-10-22 00:50:07 +02:00
|
|
|
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 );
|
|
|
|
}
|
2020-10-16 23:56:22 +02:00
|
|
|
return malloc( (size_t) size );
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:24:14 +02:00
|
|
|
#+END_SRC
|
|
|
|
|
2020-10-25 15:16:02 +01:00
|
|
|
** Test :noexport:
|
2020-10-22 01:24:14 +02:00
|
|
|
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
|
2020-10-16 23:56:22 +02:00
|
|
|
int *a;
|
2020-10-22 00:50:07 +02:00
|
|
|
a = (int*) qmckl_malloc( (qmckl_context) 1, 3*sizeof(int));
|
2020-10-16 23:56:22 +02:00
|
|
|
a[0] = 1;
|
|
|
|
a[1] = 2;
|
|
|
|
a[2] = 3;
|
2020-10-17 01:10:54 +02:00
|
|
|
munit_assert_int(a[0], ==, 1);
|
|
|
|
munit_assert_int(a[1], ==, 2);
|
|
|
|
munit_assert_int(a[2], ==, 3);
|
2020-10-22 01:24:14 +02:00
|
|
|
#+END_SRC
|
2020-10-16 23:56:22 +02:00
|
|
|
|
2020-10-22 01:24:14 +02:00
|
|
|
* =qmckl_free=
|
2020-10-16 23:56:22 +02:00
|
|
|
|
2020-10-22 01:24:14 +02:00
|
|
|
** Header
|
|
|
|
#+BEGIN_SRC C :comments link :tangle qmckl_memory.h
|
2020-10-16 23:56:22 +02:00
|
|
|
void qmckl_free(void *ptr);
|
2020-10-22 01:24:14 +02:00
|
|
|
#+END_SRC
|
2020-10-16 23:56:22 +02:00
|
|
|
|
2020-10-22 01:24:14 +02:00
|
|
|
** Source
|
|
|
|
#+BEGIN_SRC C :comments link :tangle qmckl_memory.c
|
2020-10-16 23:56:22 +02:00
|
|
|
void qmckl_free(void *ptr) {
|
|
|
|
free(ptr);
|
|
|
|
}
|
2020-10-22 01:24:14 +02:00
|
|
|
#+END_SRC
|
|
|
|
|
2020-10-25 15:16:02 +01:00
|
|
|
** Test :noexport:
|
2020-10-22 01:24:14 +02:00
|
|
|
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
|
2020-10-16 23:56:22 +02:00
|
|
|
qmckl_free(a);
|
2020-10-22 01:24:14 +02:00
|
|
|
#+END_SRC
|
2020-10-16 23:56:22 +02:00
|
|
|
|
2020-10-25 15:16:02 +01:00
|
|
|
* End of files :noexport:
|
2020-10-16 23:56:22 +02:00
|
|
|
|
2020-10-22 01:24:14 +02:00
|
|
|
** Header
|
|
|
|
#+BEGIN_SRC C :comments link :tangle qmckl_memory.h
|
2020-10-16 23:56:22 +02:00
|
|
|
#endif
|
2020-10-22 01:24:14 +02:00
|
|
|
#+END_SRC
|
2020-10-16 23:56:22 +02:00
|
|
|
|
2020-10-22 01:24:14 +02:00
|
|
|
** Test
|
|
|
|
#+BEGIN_SRC C :comments link :tangle test_qmckl_memory.c
|
2020-10-17 01:10:54 +02:00
|
|
|
return MUNIT_OK;
|
2020-10-22 01:24:14 +02:00
|
|
|
}
|
2020-10-17 01:10:54 +02:00
|
|
|
|
2020-10-22 01:24:14 +02:00
|
|
|
#+END_SRC
|