Ticket #1694: pes_allocator.patch

File pes_allocator.patch, 2.4 KB (added by Janne <janne-mythtv@…>, 20 years ago)
  • libs/libmythtv/mpeg/pespacket.cpp

     
    135145static vector<unsigned char*> free4096;
    136146static map<unsigned char*, bool> alloc4096;
    137147
    138 #define BLOCKS188 1000
     148#define BLOCKS188 512
    139149static unsigned char* get_188_block()
    140150{
    141     if (!free188.size())
     151    if (free188.empty())
    142152    {
    143153        mem188.push_back((unsigned char*) malloc(188 * BLOCKS188));
    144154        free188.reserve(BLOCKS188);
     155        unsigned char* block_start = mem188.back();
    145156        for (uint i = 0; i < BLOCKS188; ++i)
    146             free188.push_back(i*188 + mem188.back());
     157            free188.push_back(i*188 + block_start);
    147158    }
    148159
    149160    unsigned char *ptr = free188.back();
     
    161172static void return_188_block(unsigned char* ptr)
    162173{
    163174    alloc188.erase(ptr);
    164     if (alloc188.size())
    165         free188.push_back(ptr);
    166     else
     175    free188.push_back(ptr);
     176    // free the allocator only if more than 1 block was used
     177    if (alloc188.empty() && mem188.size() > 1)
    167178    {
    168179        vector<unsigned char*>::iterator it;
    169180        for (it = mem188.begin(); it != mem188.end(); ++it)
     
    174185    }
    175186}
    176187
    177 #define BLOCKS4096 256
     188#define BLOCKS4096 128
    178189static unsigned char* get_4096_block()
    179190{
    180     if (!free4096.size())
     191    if (free4096.empty())
    181192    {
    182193        mem4096.push_back((unsigned char*) malloc(4096 * BLOCKS4096));
    183194        free4096.reserve(BLOCKS4096);
     195        unsigned char* block_start = mem4096.back();
    184196        for (uint i = 0; i < BLOCKS4096; ++i)
    185             free4096.push_back(i*4096 + mem4096.back());
     197            free4096.push_back(i*4096 + block_start);
    186198    }
    187199
    188200    unsigned char *ptr = free4096.back();
     
    200212static void return_4096_block(unsigned char* ptr)
    201213{
    202214    alloc4096.erase(ptr);
    203     if (alloc4096.size())
    204     {
    205         free4096.push_back(ptr);
     215    free4096.push_back(ptr);
    206216
    207217#if 0 // enable this to debug memory leaks
    208218        cerr<<alloc4096.size()<<" 4096 blocks remain"<<endl;
     
    220230            cerr<<endl;
    221231        }
    222232#endif
    223     }
    224     else
     233
     234    // free the allocator only if more than 1 block was used
     235    if (alloc4096.empty() && mem4096.size() > 1)
    225236    {
    226237        vector<unsigned char*>::iterator it;
    227238        for (it = mem4096.begin(); it != mem4096.end(); ++it)