Ejemplo n.º 1
0
    void garbageCollect()
    {
        for(mem_iter iter = memory_map.begin();
            iter != memory_map.end(); ++iter) {

            if ((iter->second).is_free) {

                if (!(iter->second).is_unlinked) {
                    freeWrapper(iter->first);
                }
                total_bytes -= iter->second.bytes;
            }
        }

        mem_iter memory_curr = memory_map.begin();
        mem_iter memory_end  = memory_map.end();

        while(memory_curr != memory_end) {
            if (memory_curr->second.is_free) {
                memory_map.erase(memory_curr++);
            } else {
                ++memory_curr;
            }
        }
    }
Ejemplo n.º 2
0
    void memFree(T *ptr)
    {
        mem_iter iter = memory_map.find((void *)ptr);

        if (iter != memory_map.end()) {
            iter->second.is_free = true;
            used_bytes -= iter->second.bytes;
        } else {
            freeWrapper(ptr); // Free it because we are not sure what the size is
        }
    }
Ejemplo n.º 3
0
    void memFree(T *ptr)
    {
        std::lock_guard<std::mutex> lock(memory_map_mutex);

        mem_iter iter = memory_map.find((void *)ptr);

        if (iter != memory_map.end()) {
            if ((iter->second).is_unlinked) return;

            iter->second.is_free = true;
            used_bytes -= iter->second.bytes;
            used_buffers--;

        } else {
            freeWrapper(ptr); // Free it because we are not sure what the size is
        }
    }
Ejemplo n.º 4
0
    void garbageCollect()
    {
        for(mem_iter iter = memory_map.begin(); iter != memory_map.end(); iter++) {
            if ((iter->second).is_free) freeWrapper(iter->first);
        }

        mem_iter memory_curr = memory_map.begin();
        mem_iter memory_end  = memory_map.end();

        while(memory_curr != memory_end) {
            if (memory_curr->second.is_free) {
                memory_map.erase(memory_curr++);
            } else {
                ++memory_curr;
            }
        }
    }
Ejemplo n.º 5
0
int main(void)
{
    /* Declare Variables, a pain in C */
    int (* cmp) (const void*, const void*);
    char** input, ** input2, * linebuf, * linebuf2, curChar;
    size_t maxLineNum, lineNum, lineSize, bufferSize, strSize, i, j;
    int isEOF, isSpace, allocation;
    
    /* Initialize variables */
    bufferSize = INIT_SIZE, maxLineNum = INIT_SIZE;
    lineNum = 0, lineSize = 0, isEOF = feof(stdin), strSize = 0, allocation = 0;
    
    /* Set frobcmp pointer */
    cmp = &cmpWrapper;
    
    /* Setup Initial Buffer */
    linebuf = (char*) malloc(sizeof(char) * bufferSize);
    checkMemErr(linebuf, &allocation);
    input = (char**) malloc(sizeof(char*) * maxLineNum);
    checkMemErr(input, &allocation);
    
    /*                                                      *
     *  To prevent getting a char with value EOF,           *
     *  We use feof(stdin) as the condition for the loop.   *
     */
    while ( ! isEOF)
    {
        curChar = getchar();
        isSpace = curChar == SPACE;
        isEOF = feof(stdin);
        if (lineSize == 0 && isSpace)
            continue;       /* Skip consecutive spaces */
        
        /* Resize buffer */
        if (lineSize == bufferSize)
        {
            bufferSize *= 2;
            linebuf2 = (char*) realloc(linebuf, sizeof(char) * bufferSize);
            checkMemErr(linebuf2, NULL);
            linebuf = linebuf2;
        }
        
        /* Store the current character */
        if ( ! isEOF)
        {
            strSize++;
            linebuf[lineSize++] = curChar;
            if ( ! isSpace)
                continue;
        }
        else
        {
            checkIOErr(stdin);    /* Check IO Error if getchar returns EOF */
            if ( ! strSize)
            {
                freeWrapper(input, &allocation);
                freeWrapper(linebuf, &allocation);
                if (allocation)
                    reportErr("Memory Leak");   /* Check Memory Leak */
                return 0;         /* An Empty file or a file with only spaces */
            }
            /* Append a space if there is none */
            if ( ! lineSize && linebuf != NULL)
                freeWrapper(linebuf, &allocation);
            else if (lineSize && linebuf[lineSize-1] != SPACE)
                linebuf[lineSize++] = SPACE;
        }
        
        /* Reached a new line */
        input[lineNum++] = linebuf;
        linebuf = NULL;
        
        if ( ! isEOF)
        {
            /* Resize input */
            if (lineNum == maxLineNum)
            {
                maxLineNum *= 2;
                input2 = (char**) realloc(input, sizeof(char*) * maxLineNum);
                checkMemErr(input2, NULL);
                input = input2;
            }
            lineSize = 0;
            
            /* Allocate storage for a new line */
            bufferSize = INIT_SIZE;
            linebuf = (char*) malloc(sizeof(char) * bufferSize);
            checkMemErr(linebuf, &allocation);
        }
    }
    
    /* Sort the input */
    qsort(input, lineNum, sizeof(char*), cmp);
    
    /* Output results */
    for (i = 0; i < lineNum; i++)
    {
        for (j = 0, curChar = input[i][0]; curChar != SPACE; curChar = input[i][++j])
        {
            putchar(curChar);
            checkIOErr(stdout);
        }
        putchar(curChar);
        checkIOErr(stdout);
    }
    
    /* Free input array */
    for (i = 0; i < lineNum; i++)
        freeWrapper(input[i], &allocation);
    freeWrapper(input, &allocation);
    
    if (allocation)
        reportErr("Memory Leak");       /* Check Memory Leak */
    return 0;
}