Changeset 264


Ignore:
Timestamp:
Dec 6, 2002, 5:10:40 PM (18 years ago)
Author:
pooh
Message:
  • Code cleanup.
Location:
to-imperative/trunk/libp++
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/libp++/pxx_allocator.hh

    r103 r264  
    11//
    2 // Copyright (C) 2000, 2001 Andrey Slepuhin <pooh@msu.ru>
     2// Copyright (C) 2000-2002 Andrey Slepuhin <pooh@msu.ru>
    33//
    44// libp++ is free software; you can redistribute it and/or modify
     
    2121// Author: Andrey Slepuhin <pooh@msu.ru>
    2222
    23 #ifndef __pxx_i_alllocator_hh__
    24 #define __pxx_i_alllocator_hh__
     23#ifndef __pxx_alllocator_hh__
     24#define __pxx_alllocator_hh__
    2525
    2626#include "pxx_common.hh"
     27#include "pxx_sys_error.hh"
    2728
    2829namespace pxx
     
    3031
    3132//
    32 // generic allocator interface
    33 class I_Allocator
     33// Generic allocator interface
     34class Allocator
    3435{
    3536
     
    3738
    3839  //
    39   // virtual destructor
    40   virtual ~I_Allocator () {}
     40  // Constructor
     41  Allocator () {}
    4142  //
    42   // allocate memory block of _size bytes
    43   virtual void* allocate (size_t _size) = 0;
     43  // Virtual destructor
     44  virtual ~Allocator () {}
    4445  //
    45   // free memory block at _ptr
    46   virtual void deallocate (void* _ptr) = 0;
     46  // Allocate memory block of _size bytes
     47  virtual void* allocate (size_t _size) throw (SysError) = 0 ;
    4748  //
    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 ;
    5054
    5155};
  • to-imperative/trunk/libp++/pxx_heap.cc

    r115 r264  
    11//
    2 // Copyright (C) 2000, 2001 Andrey Slepuhin <pooh@msu.ru>
     2// Copyright (C) 2000-2002 Andrey Slepuhin <pooh@msu.ru>
    33//
    44// libp++ is free software; you can redistribute it and/or modify
     
    2121// Author: Andrey Slepuhin <pooh@msu.ru>
    2222
    23 #include "pxx_heap.hh"
     23#include "pxx_heap.ih"
     24#include "pxx_sys_error.ih"
    2425
    2526#include <sys/mman.h>
    2627#include <unistd.h>
     28#include <errno.h>
    2729
    2830namespace pxx
     
    3032
    3133Heap::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)
    4541{
    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))
    5151    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;
    6160  }
    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);
    6764}
    6865
    69 Heap::~Heap ()
     66Heap::~Heap () throw (SysError)
    7067{
    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);
    7670}
    7771
    78 void Heap::expand (unsigned _order = 1)
     72void Heap::expand (unsigned _order /* = 1 */) throw (SysError)
    7973{
    8074  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)))
    8276    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;
    9182  current_size = new_size;
    9283  if (fd != 0) {
    9384    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);
    9686  }
    9787}
  • to-imperative/trunk/libp++/pxx_heap.hh

    r103 r264  
    11//
    2 // Copyright (C) 2000, 2001 Andrey Slepuhin <pooh@msu.ru>
     2// Copyright (C) 2000-2002 Andrey Slepuhin <pooh@msu.ru>
    33//
    44// libp++ is free software; you can redistribute it and/or modify
     
    2525
    2626#include "pxx_common.hh"
     27#include "pxx_sys_error.hh"
    2728
    2829namespace pxx
    2930{
    3031
     32//
     33// A class representing generic mmapped block of memory of size 2^n.
    3134class Heap
    3235{
    3336
    34 protected:
     37private:
    3538  //
    36   // heap start address
    37   void* const start ;
     39  // Heap start address, if zero then address will be chosen by mmap()
     40  void* const start_addr ;
    3841  //
    39   // heap maximum size
    40   size_t const max_size ;
     42  // Maximum heap size, if zero then heap may grow without limits
     43  size_t max_size ;
    4144  //
    42   // an order of maximum size
    43   size_t max_order ;
     45  // Binary logarithm of maximum size
     46  size_t max_size_log ;
    4447  //
    45   // heap current size
     48  // Current heap size
    4649  size_t current_size ;
    4750  //
    48   // an order of current size
    49   size_t current_order ;
     51  // Binary logarithm of current size
     52  size_t cur_size_log ;
    5053  //
    51   //
     54  // File descriptor for mmapped file, if it is equal -1, then use MAP_ANON
    5255  int fd ;
    53   //
    54   //
    55   bool need_remap ;
    5656
    5757public:
    5858  //
    59   // constructor
     59  // Constructor
    6060  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) ;
    6766  //
    68   // destructor
    69   ~Heap () ;
     67  // Destructor
     68  ~Heap () throw (SysError) ;
    7069  //
    71   // expands a heap
    72   void expand (unsigned _order = 1) ;
     70  // Expand a heap
     71  void expand (unsigned _order = 1) throw (SysError) ;
    7372
    7473};
Note: See TracChangeset for help on using the changeset viewer.