Changeset 264
- Timestamp:
- Dec 6, 2002, 5:10:40 PM (18 years ago)
- Location:
- to-imperative/trunk/libp++
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
to-imperative/trunk/libp++/pxx_allocator.hh
r103 r264 1 1 // 2 // Copyright (C) 2000 , 2001Andrey Slepuhin <pooh@msu.ru>2 // Copyright (C) 2000-2002 Andrey Slepuhin <pooh@msu.ru> 3 3 // 4 4 // libp++ is free software; you can redistribute it and/or modify … … 21 21 // Author: Andrey Slepuhin <pooh@msu.ru> 22 22 23 #ifndef __pxx_ i_alllocator_hh__24 #define __pxx_ i_alllocator_hh__23 #ifndef __pxx_alllocator_hh__ 24 #define __pxx_alllocator_hh__ 25 25 26 26 #include "pxx_common.hh" 27 #include "pxx_sys_error.hh" 27 28 28 29 namespace pxx … … 30 31 31 32 // 32 // generic allocator interface33 class I_Allocator33 // Generic allocator interface 34 class Allocator 34 35 { 35 36 … … 37 38 38 39 // 39 // virtual destructor40 virtual ~I_Allocator () {}40 // Constructor 41 Allocator () {} 41 42 // 42 // allocate memory block of _size bytes43 virtual void* allocate (size_t _size) = 0;43 // Virtual destructor 44 virtual ~Allocator () {} 44 45 // 45 // free memory block at _ptr46 virtual void deallocate (void* _ptr) = 0;46 // Allocate memory block of _size bytes 47 virtual void* allocate (size_t _size) throw (SysError) = 0 ; 47 48 // 48 // resize memory block at _ptr to _size bytes 49 virtual void* reallocate (void* _ptr, size_t _size) = 0; 49 // Free memory block at _ptr 50 virtual void deallocate (void* _ptr) = 0 ; 51 // 52 // Resize memory block at _ptr to _size bytes 53 virtual void* reallocate (void* _ptr, size_t _size) throw (SysError) = 0 ; 50 54 51 55 }; -
to-imperative/trunk/libp++/pxx_heap.cc
r115 r264 1 1 // 2 // Copyright (C) 2000 , 2001Andrey Slepuhin <pooh@msu.ru>2 // Copyright (C) 2000-2002 Andrey Slepuhin <pooh@msu.ru> 3 3 // 4 4 // libp++ is free software; you can redistribute it and/or modify … … 21 21 // Author: Andrey Slepuhin <pooh@msu.ru> 22 22 23 #include "pxx_heap.hh" 23 #include "pxx_heap.ih" 24 #include "pxx_sys_error.ih" 24 25 25 26 #include <sys/mman.h> 26 27 #include <unistd.h> 28 #include <errno.h> 27 29 28 30 namespace pxx … … 30 32 31 33 Heap::Heap ( 32 void* const _start, 33 size_t const _max_size, 34 size_t _init_size = 0, 35 int _fd = 0, 36 bool _need_remap = true 37 ) : 38 start (_start), 39 max_size (_max_size), 40 max_order (get_order(max_size)), 41 current_size (_init_size == 0 ? page_size : _init_size), 42 current_order (get_order(current_size)), 43 fd (_fd), 44 need_remap (_need_remap) 34 size_t _init_size /* = 0 */, // Initial heap size, page size by default 35 size_t const _max_size /* = 0 */, // Maximum size, not limited by default 36 void* const _start /* = null */, 37 int _fd /* = 0 */ 38 ) throw (SysError) : 39 start_addr (_start), 40 fd (_fd) 45 41 { 46 if (rounds[max_order] != max_size) 47 FATAL("heap size is not a power of 2"); 48 if (!aligned(_start, page_size)) 49 FATAL("Heap start address is bad aligned"); 50 if (current_size > max_size) 42 cur_size_log = get_order(_init_size == 0 ? page_size : _init_size); 43 current_size = 1 << cur_size_log; 44 if (_max_size != 0) { 45 max_size_log = get_order(_max_size); 46 max_size = 1 << max_size_log; 47 } else { 48 max_size = max_size_log = 0; 49 } 50 if ((max_size != 0) && (current_size > max_size)) 51 51 FATAL("Initial heap size greater than maximal size"); 52 if (need_remap) { 53 void* res = mmap( 54 start, current_size, 55 PROT_READ | PROT_WRITE, 56 (fd == 0 ? MAP_PRIVATE | MAP_ANON : MAP_SHARED) | MAP_FIXED, 57 fd, 0 58 ); 59 if (res == MAP_FAILED) 60 FATAL("mmap() failed, errno = %d", errno); 52 int mmap_flags = 0; 53 if (start_addr != null) mmap_flags |= MAP_FIXED; 54 if (fd != -1) { 55 mmap_flags |= MAP_SHARED; 56 int res = ftruncate(fd, current_size); 57 if (res != 0) throw_sys_error(errno); 58 } else { 59 mmap_flags |= MAP_PRIVATE | MAP_ANON; 61 60 } 62 if (fd != 0) { 63 int res = ftruncate(fd, current_size); 64 if (res != 0) 65 FATAL("ftruncate() failed, errno = %d", errno); 66 } 61 void* res = 62 mmap(start_addr, current_size, PROT_READ | PROT_WRITE, mmap_flags, fd, 0); 63 if (res == MAP_FAILED) throw_sys_error(errno); 67 64 } 68 65 69 Heap::~Heap () 66 Heap::~Heap () throw (SysError) 70 67 { 71 if (need_remap) { 72 int res = munmap(start, current_size); 73 if (res != 0) 74 FATAL("munmap() failed, errno = %d", errno); 75 } 68 int res = munmap(start_addr, current_size); 69 if (res != 0) throw_sys_error(errno); 76 70 } 77 71 78 void Heap::expand (unsigned _order = 1)72 void Heap::expand (unsigned _order /* = 1 */) throw (SysError) 79 73 { 80 74 size_t new_size = current_size << _order; 81 if (( new_size > max_size) || (new_size == 0))75 if ((max_size != 0) && ((new_size > max_size) || (new_size == 0))) 82 76 FATAL("Heap exhausted"); 83 if (need_remap) { 84 void* res = mremap(start, current_size, new_size, 0); 85 if (res == MAP_FAILED) 86 FATAL("mremap() failed, errno = %d", errno); 87 if (res != start) 88 FATAL("mremap() changes a heap start address"); 89 } 90 current_order += _order; 77 void* res = mremap(start_addr, current_size, new_size, 0); 78 if (res == MAP_FAILED) throw_sys_error(errno); 79 if (res != start_addr) 80 FATAL("mremap() changes a heap start address"); 81 cur_size_log += _order; 91 82 current_size = new_size; 92 83 if (fd != 0) { 93 84 int res = ftruncate(fd, current_size); 94 if (res != 0) 95 FATAL("ftruncate() failed, errno = %d", errno); 85 if (res != 0) throw_sys_error(errno); 96 86 } 97 87 } -
to-imperative/trunk/libp++/pxx_heap.hh
r103 r264 1 1 // 2 // Copyright (C) 2000 , 2001Andrey Slepuhin <pooh@msu.ru>2 // Copyright (C) 2000-2002 Andrey Slepuhin <pooh@msu.ru> 3 3 // 4 4 // libp++ is free software; you can redistribute it and/or modify … … 25 25 26 26 #include "pxx_common.hh" 27 #include "pxx_sys_error.hh" 27 28 28 29 namespace pxx 29 30 { 30 31 32 // 33 // A class representing generic mmapped block of memory of size 2^n. 31 34 class Heap 32 35 { 33 36 34 pr otected:37 private: 35 38 // 36 // heap start address37 void* const start ;39 // Heap start address, if zero then address will be chosen by mmap() 40 void* const start_addr ; 38 41 // 39 // heap maximum size40 size_t constmax_size ;42 // Maximum heap size, if zero then heap may grow without limits 43 size_t max_size ; 41 44 // 42 // an orderof maximum size43 size_t max_ order;45 // Binary logarithm of maximum size 46 size_t max_size_log ; 44 47 // 45 // heap currentsize48 // Current heap size 46 49 size_t current_size ; 47 50 // 48 // an orderof current size49 size_t cur rent_order;51 // Binary logarithm of current size 52 size_t cur_size_log ; 50 53 // 51 // 54 // File descriptor for mmapped file, if it is equal -1, then use MAP_ANON 52 55 int fd ; 53 //54 //55 bool need_remap ;56 56 57 57 public: 58 58 // 59 // constructor59 // Constructor 60 60 Heap ( 61 void* const _start, 62 size_t const _max_size, 63 size_t _init_size = 0, 64 int _fd = 0, 65 bool _need_remap = true 66 ) ; 61 size_t _init_size = 0, // Initial heap size, page size by default 62 size_t const _max_size = 0, // Maximum size, not limited by default 63 void* const _start = null, 64 int _fd = 0 65 ) throw (SysError) ; 67 66 // 68 // destructor69 ~Heap () ;67 // Destructor 68 ~Heap () throw (SysError) ; 70 69 // 71 // expandsa heap72 void expand (unsigned _order = 1) ;70 // Expand a heap 71 void expand (unsigned _order = 1) throw (SysError) ; 73 72 74 73 };
Note: See TracChangeset
for help on using the changeset viewer.