… | |
… | |
43 | g_slice_free1 (s, p); |
43 | g_slice_free1 (s, p); |
44 | } |
44 | } |
45 | }; |
45 | }; |
46 | |
46 | |
47 | // strictly the same as g_slice_alloc, but never returns 0 |
47 | // strictly the same as g_slice_alloc, but never returns 0 |
48 | void *salloc (int size) throw (std::bad_alloc); |
48 | void *salloc (int size) throw (std::bad_alloc); |
49 | // also copies src into the new area, like "memdup" |
49 | // also copies src into the new area, like "memdup" |
|
|
50 | // if src is 0, clears the memory |
50 | void *salloc (int size, void *src) throw (std::bad_alloc); |
51 | void *salloc (int size, void *src) throw (std::bad_alloc); |
|
|
52 | |
|
|
53 | // and as a template |
|
|
54 | template<typename T> |
|
|
55 | inline T *salloc (int size) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T)); } |
|
|
56 | template<typename T> |
|
|
57 | inline T *salloc (int size, T *src) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T), (void *)src); } |
|
|
58 | |
51 | // for symmetry |
59 | // for symmetry |
|
|
60 | template<typename T> |
52 | inline void sfree (void *ptr, int size) throw () |
61 | inline void sfree (T *ptr, int size) throw () |
53 | { |
62 | { |
54 | g_slice_free1 (size, ptr); |
63 | g_slice_free1 (size * sizeof (T), (void *)ptr); |
55 | } |
64 | } |
56 | |
65 | |
57 | // a STL-compatible allocator that uses g_slice |
66 | // a STL-compatible allocator that uses g_slice |
58 | // boy, this is verbose |
67 | // boy, this is verbose |
59 | template<typename Tp> |
68 | template<typename Tp> |
… | |
… | |
83 | pointer address (reference x) const { return &x; } |
92 | pointer address (reference x) const { return &x; } |
84 | const_pointer address (const_reference x) const { return &x; } |
93 | const_pointer address (const_reference x) const { return &x; } |
85 | |
94 | |
86 | pointer allocate (size_type n, const_pointer = 0) |
95 | pointer allocate (size_type n, const_pointer = 0) |
87 | { |
96 | { |
88 | return static_cast<pointer>(salloc (n * sizeof (Tp))); |
97 | return salloc<Tp> (n); |
89 | } |
98 | } |
90 | |
99 | |
91 | void deallocate (pointer p, size_type n) |
100 | void deallocate (pointer p, size_type n) |
92 | { |
101 | { |
93 | sfree (static_cast<void *>(p), n * sizeof (Tp)); |
102 | sfree (p, n); |
94 | } |
103 | } |
95 | |
104 | |
96 | size_type max_size ()const throw () |
105 | size_type max_size ()const throw () |
97 | { |
106 | { |
98 | return size_t (-1) / sizeof (Tp); |
107 | return size_t (-1) / sizeof (Tp); |