monotone

monotone Mtn Source Tree

Root/botan/defalloc.cpp

1/*************************************************
2* Basic Allocators Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/defalloc.h>
7#include <botan/util.h>
8#include <cstdlib>
9#include <cstring>
10
11namespace Botan {
12
13namespace {
14
15/*************************************************
16* Perform Memory Allocation *
17*************************************************/
18void* do_malloc(u32bit n, bool do_lock)
19 {
20 void* ptr = std::malloc(n);
21
22 if(!ptr)
23 return 0;
24
25 if(do_lock)
26 lock_mem(ptr, n);
27
28 std::memset(ptr, 0, n);
29 return ptr;
30 }
31
32/*************************************************
33* Perform Memory Deallocation *
34*************************************************/
35void do_free(void* ptr, u32bit n, bool do_lock)
36 {
37 if(!ptr)
38 return;
39
40 std::memset(ptr, 0, n);
41 if(do_lock)
42 unlock_mem(ptr, n);
43
44 std::free(ptr);
45 }
46
47}
48
49/*************************************************
50* Malloc_Allocator's Allocation *
51*************************************************/
52void* Malloc_Allocator::alloc_block(u32bit n) const
53 {
54 return do_malloc(n, false);
55 }
56
57/*************************************************
58* Malloc_Allocator's Deallocation *
59*************************************************/
60void Malloc_Allocator::dealloc_block(void* ptr, u32bit n) const
61 {
62 do_free(ptr, n, false);
63 }
64
65/*************************************************
66* Locking_Allocator's Allocation *
67*************************************************/
68void* Locking_Allocator::alloc_block(u32bit n) const
69 {
70 return do_malloc(n, true);
71 }
72
73/*************************************************
74* Locking_Allocator's Deallocation *
75*************************************************/
76void Locking_Allocator::dealloc_block(void* ptr, u32bit n) const
77 {
78 do_free(ptr, n, true);
79 }
80
81}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status