monotone

monotone Mtn Source Tree

Root/botan/defalloc.cpp

1/*************************************************
2* Basic Allocators Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/defalloc.h>
7#include <botan/libstate.h>
8#include <botan/util.h>
9#include <cstdlib>
10#include <cstring>
11
12namespace Botan {
13
14namespace {
15
16/*************************************************
17* Perform Memory Allocation *
18*************************************************/
19void* do_malloc(u32bit n, bool do_lock)
20 {
21 void* ptr = std::malloc(n);
22
23 if(!ptr)
24 return 0;
25
26 if(do_lock)
27 lock_mem(ptr, n);
28
29 std::memset(ptr, 0, n);
30 return ptr;
31 }
32
33/*************************************************
34* Perform Memory Deallocation *
35*************************************************/
36void do_free(void* ptr, u32bit n, bool do_lock)
37 {
38 if(!ptr)
39 return;
40
41 std::memset(ptr, 0, n);
42 if(do_lock)
43 unlock_mem(ptr, n);
44
45 std::free(ptr);
46 }
47
48}
49
50/*************************************************
51* Malloc_Allocator's Allocation *
52*************************************************/
53void* Malloc_Allocator::allocate(u32bit n)
54 {
55 return do_malloc(n, false);
56 }
57
58/*************************************************
59* Malloc_Allocator's Deallocation *
60*************************************************/
61void Malloc_Allocator::deallocate(void* ptr, u32bit n)
62 {
63 do_free(ptr, n, false);
64 }
65
66/*************************************************
67* Locking_Allocator's Allocation *
68*************************************************/
69void* Locking_Allocator::alloc_block(u32bit n)
70 {
71 return do_malloc(n, true);
72 }
73
74/*************************************************
75* Locking_Allocator's Deallocation *
76*************************************************/
77void Locking_Allocator::dealloc_block(void* ptr, u32bit n)
78 {
79 do_free(ptr, n, true);
80 }
81
82/*************************************************
83* Get an allocator *
84*************************************************/
85Allocator* Allocator::get(bool locking)
86 {
87 std::string type = "";
88 if(!locking)
89 type = "malloc";
90
91 Allocator* alloc = global_state().get_allocator(type);
92 if(alloc)
93 return alloc;
94
95 throw Exception("Couldn't find an allocator to use in get_allocator");
96 }
97
98}

Archive Download this file

Branches

Tags

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