FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
memory_aux_main.cpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6// includes, FEAT
7#include <kernel/util/memory_aux.hpp>
8#include <kernel/util/random.hpp>
9
10// includes, system
11#include <memory>
12#include <cstring>
13
14namespace FEAT
15{
16 namespace Memory
17 {
18 #ifndef FEAT_HAVE_CUDA
19 void* alloc_main(std::size_t bytes)
20 {
21 return malloc(bytes);
22 }
23 #endif
24
25 #ifndef FEAT_HAVE_CUDA
26 void free_main(void* ptr)
27 {
28 free(ptr);
29 }
30 #endif
31
32 void memset_main(void* ptr, int value, std::size_t bytes)
33 {
34 if(bytes <= std::size_t(0))
35 return;
36
37 XASSERT(ptr != nullptr);
38
39 ::memset(ptr, value, bytes);
40 }
41
42 void memset_random_main(void* ptr, std::size_t bytes, std::uint64_t seed)
43 {
44 if(bytes <= std::size_t(0))
45 return;
46
47 XASSERT(ptr != nullptr);
48
49 // zero seed? replace by pointer address then
50 if(seed == 0u)
51 seed = reinterpret_cast<std::uint64_t>(ptr);
52
53 // initialize RNG
54 Random rng(seed);
55
56 // cast array to 64 bit int and fill them with random values
57 std::uint64_t* x = reinterpret_cast<std::uint64_t*>(ptr);
58 std::size_t n = bytes / 8u;
59 for(std::size_t i = 0; i < n; ++i, ++x)
60 *x = rng.next();
61
62 // there may be a few (<8) bytes left, so format them via a memcpy
63 std::size_t bytes_left = bytes & 0x7u;
64 if(bytes_left > 0u)
65 {
66 std::uint64_t t = rng.next();
67 ::memcpy(x, &t, bytes_left);
68 }
69 }
70
71 void memcopy_main(void* dst, const void* src, std::size_t bytes)
72 {
73 if(bytes <= std::size_t(0))
74 return;
75
76 XASSERT(dst != nullptr);
77 XASSERT(src != nullptr);
78
79 if(dst == src)
80 return;
81
82 // check for overlapping memory regions
83 XASSERTM((dst < src) || ((const char*)src + bytes <= dst), "source and destination memory regions overlap");
84 XASSERTM((src < dst) || ((const char*)dst + bytes <= src), "source and destination memory regions overlap");
85
86 ::memcpy(dst, src, bytes);
87 }
88 } // namespace Memory
89} // namespace FEAT
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Pseudo-Random Number Generator.
Definition: random.hpp:54
uint64_t next()
Returns the next number in the stream.
Definition: random.hpp:106
void memcopy_main(void *dst, const void *src, std::size_t bytes)
Copies a region of a main memory block into another main memory block.
void memset_main(void *ptr, int value, std::size_t bytes)
Formats all bytes of a memory block in main memory to a given value.
void free_main(void *ptr)
Frees a memory block in main heap memory allocated by alloc_main.
void memset_random_main(void *ptr, std::size_t bytes, std::uint64_t seed=0u)
Formats all bytes of a memory block in main memory to random values.
void * alloc_main(std::size_t bytes)
Allocate a memory block in main heap memory.
FEAT namespace.
Definition: adjactor.hpp:12
@ value
specifies whether the space should supply basis function values