1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : gb_storage.h // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #ifndef GB_STORAGE_H |
---|
12 | #define GB_STORAGE_H |
---|
13 | |
---|
14 | #ifndef GB_DATA_H |
---|
15 | #include "gb_data.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | CONSTEXPR_INLINE bool store_inside_entry(int size, int memsize) { |
---|
19 | // returns true if data can be stored inside GBENTRY |
---|
20 | return size<256 && memsize<SIZOFINTERN; |
---|
21 | } |
---|
22 | |
---|
23 | inline char *GBENTRY::alloc_data(long Size, long Memsize) { |
---|
24 | char *mem; |
---|
25 | gb_assert(!flags2.is_indexed); |
---|
26 | gb_assert(implicated(stored_external(), !data())); // would leak memory |
---|
27 | |
---|
28 | if (store_inside_entry(Size, Memsize)) { |
---|
29 | mark_as_intern(); |
---|
30 | |
---|
31 | info.istr.size = (unsigned char)Size; |
---|
32 | info.istr.memsize = (unsigned char)Memsize; |
---|
33 | |
---|
34 | mem = data(); |
---|
35 | } |
---|
36 | else { |
---|
37 | mark_as_extern(); |
---|
38 | |
---|
39 | info.ex.size = Size; |
---|
40 | info.ex.memsize = Memsize; |
---|
41 | |
---|
42 | mem = (char*)gbm_get_mem((size_t)Memsize, GB_GBM_INDEX(this)); |
---|
43 | info.ex.set_data(mem); |
---|
44 | } |
---|
45 | return mem; |
---|
46 | } |
---|
47 | |
---|
48 | class GBENTRY_memory : virtual Noncopyable { |
---|
49 | GBENTRY *gbe; |
---|
50 | char *mem; |
---|
51 | public: |
---|
52 | GBENTRY_memory(GBENTRY *gb_entry, long size, long memsize) |
---|
53 | : gbe(gb_entry), |
---|
54 | mem(gbe->alloc_data(size, memsize)) |
---|
55 | {} |
---|
56 | ~GBENTRY_memory() { gbe->index_re_check_in(); } |
---|
57 | operator char*() { return mem; } |
---|
58 | }; |
---|
59 | |
---|
60 | inline void GBENTRY::insert_data(const char *Data, long Size, long Memsize) { |
---|
61 | gb_assert(Data); |
---|
62 | memcpy(GBENTRY_memory(this, Size, Memsize), Data, Memsize); |
---|
63 | } |
---|
64 | |
---|
65 | #else |
---|
66 | #error gb_storage.h included twice |
---|
67 | #endif // GB_STORAGE_H |
---|