1 /*===========================================================================
2 *
3 *                            PUBLIC DOMAIN NOTICE
4 *               National Center for Biotechnology Information
5 *
6 *  This software/database is a "United States Government Work" under the
7 *  terms of the United States Copyright Act.  It was written as part of
8 *  the author's official duties as a United States Government employee and
9 *  thus cannot be copyrighted.  This software/database is freely available
10 *  to the public for use. The National Library of Medicine and the U.S.
11 *  Government have not placed any restriction on its use or reproduction.
12 *
13 *  Although all reasonable efforts have been taken to ensure the accuracy
14 *  and reliability of the software and data, the NLM and the U.S.
15 *  Government do not and cannot warrant the performance or results that
16 *  may be obtained by using this software or data. The NLM and the U.S.
17 *  Government disclaim all warranties, express or implied, including
18 *  warranties of performance, merchantability or fitness for any particular
19 *  purpose.
20 *
21 *  Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26 
27 #include <kfc/memmgr.hpp>
28 #include <kfc/memory.hpp>
29 #include <kfc/rsrc.hpp>
30 #include <kfc/caps.hpp>
31 #include <kfc/callstk.hpp>
32 
33 namespace vdb3
34 {
35 
36     /*------------------------------------------------------------------
37      * MemMgrItf
38      *  memory manager interface
39      */
40 
41     // support for C++ new and delete
_new(size_t bytes)42     void * MemMgrItf :: _new ( size_t bytes )
43     {
44         FUNC_ENTRY ();
45 
46         // allocate new object plus header size
47         Refcount * obj = ( Refcount * ) _alloc ( bytes, true );
48         obj -> mmgr = ( MemMgrItf * ) duplicate ();
49         obj -> obj_size = bytes;
50 
51         // return allocation
52         return ( void * ) obj;
53     }
54 
_delete(void * ptr)55     void MemMgrItf :: _delete ( void * ptr )
56     {
57         Refcount * obj;
58 
59         if ( ( size_t ) ptr >= sizeof * obj )
60         {
61             FUNC_ENTRY ();
62 
63             // convert pointer back to Refcount
64             obj = ( Refcount * ) ptr;
65 
66             // recover the original memory manager interface
67             MemMgrItf * self = obj -> mmgr;
68             assert ( self != 0 );
69 
70             // free the memory
71             self -> _free ( ( void * ) obj, obj -> obj_size );
72 
73             // detach from memmgr
74             self -> release ();
75         }
76     }
77 
make_mmgr_ref(Refcount * obj,caps_t caps)78     MemMgr MemMgrItf :: make_mmgr_ref ( Refcount * obj, caps_t caps )
79     {
80         // TBD - can set max capabilities here
81         return MemMgr ( obj, this, caps );
82     }
83 
make_mem_ref(Refcount * obj,MemoryItf * itf,caps_t caps)84     Mem MemMgrItf :: make_mem_ref ( Refcount * obj, MemoryItf * itf, caps_t caps )
85     {
86         // TBD - can set max capabilities here
87         return Mem ( obj, itf, caps );
88     }
89 
90 
91     /*------------------------------------------------------------------
92      * MemMgr
93      *  memory manager reference
94      */
95 
96     // allocate memory
alloc(const bytes_t & size,bool clear) const97     Mem MemMgr :: alloc ( const bytes_t & size, bool clear ) const
98     {
99         FUNC_ENTRY ();
100         MemMgrItf * itf = get_itf ( CAP_ALLOC );
101         return itf -> alloc ( size, clear );
102     }
103 
104 
105     // make a block of constant memory
make_const(const void * ptr,const bytes_t & size) const106     Mem MemMgr :: make_const ( const void * ptr, const bytes_t & size ) const
107     {
108         FUNC_ENTRY ();
109 
110         if ( ptr == 0 && size != ( U64 ) 0 )
111         {
112             if ( rsrc == 0 )
113                 throw "null const memory block";
114             THROW ( xc_null_param_err, "null pointer with size %lu", ( U64 ) size );
115         }
116 
117         MemMgrItf * itf = get_itf ( CAP_ALLOC );
118         return itf -> make_const ( ptr, size );
119     }
120 
121     // C++
MemMgr()122     MemMgr :: MemMgr ()
123     {
124     }
125 
MemMgr(const MemMgr & r)126     MemMgr :: MemMgr ( const MemMgr & r )
127         : Ref < MemMgrItf > ( r )
128     {
129     }
130 
operator =(const MemMgr & r)131     void MemMgr :: operator = ( const MemMgr & r )
132     {
133         Ref < MemMgrItf > :: operator = ( r );
134     }
135 
MemMgr(const MemMgr & r,caps_t reduce)136     MemMgr :: MemMgr ( const MemMgr & r, caps_t reduce )
137         : Ref < MemMgrItf > ( r, reduce )
138     {
139     }
140 
141     // support for "new" and "delete" operators
_new(size_t bytes) const142     void * MemMgr :: _new ( size_t bytes ) const
143     {
144         FUNC_ENTRY ();
145         MemMgrItf * itf = get_itf ( CAP_ALLOC );
146         return itf -> _new ( bytes );
147     }
148 
_delete(void * ptr) const149     void MemMgr :: _delete ( void * ptr ) const
150     {
151         FUNC_ENTRY ();
152         return MemMgrItf :: _delete ( ptr );
153     }
154 
155     // initialization by MemMgrItf
MemMgr(Refcount * obj,MemMgrItf * itf,caps_t caps)156     MemMgr :: MemMgr ( Refcount * obj, MemMgrItf * itf, caps_t caps )
157         : Ref < MemMgrItf > ( obj, itf, caps )
158     {
159     }
160 
161 
162 }
163