Ticket #12620: 0006-remplacement-BList-par-TODO.patch

File 0006-remplacement-BList-par-TODO.patch, 7.0 KB (added by Anarchos, 8 years ago)

Solving a TODO in ServerMemoryAllocator (UNTESTED)

  • headers/private/app/ServerMemoryAllocator.h

    From 8133000016aa69c624ef7098b824f60e870a6927 Mon Sep 17 00:00:00 2001
    From: Sylvain Kerjean <Sylvain_Kerjean@hotmail.com>
    Date: Fri, 22 Jan 2016 11:07:47 +0100
    Subject: [PATCH 6/6] remplacement BList par TODO
    
    ---
     headers/private/app/ServerMemoryAllocator.h |   5 +-
     src/kits/app/ServerMemoryAllocator.cpp      | 168 ++++++++++++++--------------
     2 files changed, 87 insertions(+), 86 deletions(-)
    
    diff --git a/headers/private/app/ServerMemoryAllocator.h b/headers/private/app/ServerMemoryAllocator.h
    index aea3da0..237a966 100644
    a b  
    1010
    1111
    1212#include <OS.h>
    13 #include <List.h>
    1413
     14#include <map>
     15#include <utility>
    1516
    1617namespace BPrivate {
    1718
    public:  
    3233                                    area_id& area, uint8*& base);
    3334
    3435private:
    35             BList               fAreas;
     36            std::map< area_id, std::pair<area_id, uint8*> > fAreas;
    3637};
    3738
    3839
  • src/kits/app/ServerMemoryAllocator.cpp

    diff --git a/src/kits/app/ServerMemoryAllocator.cpp b/src/kits/app/ServerMemoryAllocator.cpp
    index 6d34fc2..ab24cba 100644
    a b  
    33 * Distributed under the terms of the MIT License.
    44 *
    55 * Authors:
    6  *      Axel Dörfler, axeld@pinc-software.de
     6 *        Axel Dörfler, axeld@pinc-software.de
    77 */
    88
    99
    10 /*! Note, this class don't provide any locking whatsoever - you are
    11     supposed to have a BPrivate::AppServerLink object around which
    12     does the necessary locking.
    13     However, this is not enforced in the methods here, you have to
    14     take care for yourself!
     10/*!    Note, this class don't provide any locking whatsoever - you are
     11    supposed to have a BPrivate::AppServerLink object around which
     12    does the necessary locking.
     13    However, this is not enforced in the methods here, you have to
     14    take care for yourself!
    1515*/
    1616
    1717
    1818#include "ServerMemoryAllocator.h"
    1919
    2020#include <new>
     21#include <map>
     22#include <utility>
    2123
    2224#ifndef HAIKU_TARGET_PLATFORM_LIBBE_TEST
    23 #   include <syscalls.h>
     25#    include <syscalls.h>
    2426#endif
    2527
    2628
    static const size_t kReserveMaxSize = 32 * 1024 * 1024;  
    3032
    3133namespace BPrivate {
    3234
    33 
     35/*
    3436struct area_mapping {
    35     area_id server_area;
    36     area_id local_area;
    37     uint8*  local_base;
     37    area_id    server_area;
     38    area_id local_area;
     39    uint8*    local_base;
    3840};
    39 
     41*/
    4042
    4143ServerMemoryAllocator::ServerMemoryAllocator()
    42     :
    43     fAreas(4)
     44    :
     45    fAreas()
    4446{
    4547}
    4648
    4749
    4850ServerMemoryAllocator::~ServerMemoryAllocator()
    4951{
    50     for (int32 i = fAreas.CountItems(); i-- > 0;) {
    51         area_mapping* mapping = (area_mapping*)fAreas.ItemAt(i);
    52 
    53         delete_area(mapping->local_area);
    54         delete mapping;
    55     }
    5652}
    5753
    5854
    5955status_t
    6056ServerMemoryAllocator::InitCheck()
    6157{
    62     return B_OK;
     58    return B_OK;
    6359}
    6460
    6561
    6662status_t
    6763ServerMemoryAllocator::AddArea(area_id serverArea, area_id& _area,
    68     uint8*& _base, size_t size, bool readOnly)
     64    uint8*& _base, size_t size, bool readOnly)
    6965{
    70     area_mapping* mapping = new (std::nothrow) area_mapping;
    71     if (mapping == NULL || !fAreas.AddItem(mapping)) {
    72         delete mapping;
    73         return B_NO_MEMORY;
    74     }
    75 
    76     status_t status = B_ERROR;
    77     uint32 addressSpec = B_ANY_ADDRESS;
    78     void* base;
     66    area_id server_area, local_area;
     67    uint8*  local_base;
     68/*    area_mapping* mapping = new (std::nothrow) area_mapping;
     69
     70    if (mapping == NULL) {
     71        delete mapping;
     72        return B_NO_MEMORY;
     73    }
     74*/
     75    status_t status = B_ERROR;
     76    uint32 addressSpec = B_ANY_ADDRESS;
     77    void* base;
    7978#ifndef HAIKU_TARGET_PLATFORM_LIBBE_TEST
    80     if (!readOnly && size < kReserveMaxSize) {
    81         // Reserve 128 MB of space for the area, but only if the area
    82         // is smaller than 32 MB (else the address space waste would
    83         // likely to be too large)
    84         base = (void*)0x60000000;
    85         status = _kern_reserve_address_range((addr_t*)&base, B_BASE_ADDRESS,
    86             kReservedSize);
    87         addressSpec = status == B_OK ? B_EXACT_ADDRESS : B_BASE_ADDRESS;
    88     }
     79    if (!readOnly && size < kReserveMaxSize) {
     80        // Reserve 128 MB of space for the area, but only if the area
     81        // is smaller than 32 MB (else the address space waste would
     82        // likely to be too large)
     83        base = (void*)0x60000000;
     84        status = _kern_reserve_address_range((addr_t*)&base, B_BASE_ADDRESS,
     85            kReservedSize);
     86        addressSpec = status == B_OK ? B_EXACT_ADDRESS : B_BASE_ADDRESS;
     87    }
    8988#endif
    9089
    91     mapping->local_area = clone_area(readOnly
    92             ? "server read-only memory" : "server_memory", &base, addressSpec,
    93         B_READ_AREA | (readOnly ? 0 : B_WRITE_AREA), serverArea);
    94     if (mapping->local_area < B_OK) {
    95         status = mapping->local_area;
    96 
    97         fAreas.RemoveItem(mapping);
    98         delete mapping;
    99 
    100         return status;
    101     }
    102 
    103     mapping->server_area = serverArea;
    104     mapping->local_base = (uint8*)base;
    105 
    106     _area = mapping->local_area;
    107     _base = mapping->local_base;
    108 
    109     return B_OK;
     90    local_area = clone_area(readOnly
     91            ? "server read-only memory" : "server_memory", &base, addressSpec,
     92        B_READ_AREA | (readOnly ? 0 : B_WRITE_AREA), serverArea);
     93    if (local_area < B_OK) {
     94        status = local_area;
     95
     96        //delete mapping;
     97
     98        return status;
     99    }
     100
     101    server_area = serverArea;
     102    local_base = (uint8*)base;
     103   
     104    std::pair<std::map<area_id, std::pair<area_id, uint8*> >::iterator,bool> ret_insert;   
     105
     106    ret_insert = fAreas.insert(std::pair<area_id, std::pair<area_id, uint8*> >(server_area,
     107                       std::pair<area_id, uint8*>(local_area, local_base)));
     108    if (!ret_insert.second) {
     109//        delete mapping;
     110        return B_NO_MEMORY;
     111    }
     112   
     113    _area = local_area;
     114    _base = local_base;
     115
     116    return B_OK;
    110117}
    111118
    112119
    113120void
    114121ServerMemoryAllocator::RemoveArea(area_id serverArea)
    115122{
    116     for (int32 i = fAreas.CountItems(); i-- > 0;) {
    117         area_mapping* mapping = (area_mapping*)fAreas.ItemAt(i);
    118 
    119         if (mapping->server_area == serverArea) {
    120             // we found the area we should remove
    121             delete_area(mapping->local_area);
    122             delete mapping;
    123             fAreas.RemoveItem(i);
    124             break;
    125         }
    126     }
     123    std::map<area_id, std::pair<area_id, uint8*> >::iterator area_it = fAreas.find(serverArea);
     124
     125    if (area_it != fAreas.end()) {
     126        // we found the area we should remove
     127        std::pair<area_id, uint8*> area = area_it->second;
     128        delete_area(area.first);
     129        fAreas.erase(serverArea);
     130    }
    127131}
    128132
    129133
    130134status_t
    131135ServerMemoryAllocator::AreaAndBaseFor(area_id serverArea, area_id& _area,
    132     uint8*& _base)
     136    uint8*& _base)
    133137{
    134     // TODO: why not use a map?
    135     for (int32 i = fAreas.CountItems(); i-- > 0;) {
    136         area_mapping* mapping = (area_mapping*)fAreas.ItemAt(i);
    137 
    138         if (mapping->server_area == serverArea) {
    139             _area = mapping->local_area;
    140             _base = mapping->local_base;
    141             return B_OK;
    142         }
    143     }
    144 
    145     return B_ERROR;
     138    std::map<area_id, std::pair<area_id, uint8*> >::iterator area_it = fAreas.find(serverArea);
     139    if (area_it != fAreas.end()) {
     140        std::pair<area_id, uint8*> area = area_it->second;
     141        _area = area.first;
     142        _base = area.second;
     143        return B_OK;
     144    }
     145    return B_ERROR;
    146146}
    147147
    148148
    149 }   // namespace BPrivate
     149}    // namespace BPrivate