New Ticket     Tickets     Wiki     Browse Source     Timeline     Roadmap     Ticket Reports     Search

Changeset 80425


Ignore:
Timestamp:
07/12/11 10:21:45 (4 years ago)
Author:
cal@…
Message:

rev-upgrade: Added hashmap to libmachista to prevent unnecessary I/O

Location:
branches/gsoc11-rev-upgrade/base/src/libmachista1.0
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/gsoc11-rev-upgrade/base/src/libmachista1.0/Makefile

    r80152 r80425  
    1 OBJS=           libmachista.o 
     1OBJS=           libmachista.o hashmap.o 
    22SHLIB_NAME=     libmachista${SHLIB_SUFFIX} 
    33INSTALLDIR=     ${DESTDIR}${datadir}/macports/Tcl/libmachista1.0 
  • branches/gsoc11-rev-upgrade/base/src/libmachista1.0/libmachista.c

    r80151 r80425  
    4949 
    5050#include "libmachista.h" 
     51#include "hashmap.h" 
    5152 
    5253typedef struct macho_input { 
     
    5758/* This is macho_handle_t. The corresponding typedef is in the header */ 
    5859struct macho_handle { 
    59     /* this isn't handled as a linked list on purpose, because the mht_results are given out to the 
    60      * user and should not have a next pointer (as it doesn't make any sense in that context */ 
    61     size_t mht_result_count; 
    62     macho_t **mht_results; 
     60    HashMap *result_map; 
    6361}; 
    6462 
     
    399397 
    400398/* Parse a (possible Mach-O) file. For a more detailed description, see the header */ 
    401 int macho_parse_file(macho_handle_t *handle, const char *filepath, macho_t **res) { 
     399int macho_parse_file(macho_handle_t *handle, const char *filepath, const macho_t **res) { 
    402400    int fd; 
    403401    struct stat st; 
    404402    void *data; 
    405403    macho_input_t input_file; 
     404 
     405    /* Check hashmap for precomputed results */ 
     406    const macho_t *cached_res = hashMapGet(handle->result_map, filepath); 
     407    if (cached_res != NULL) { 
     408        *res = cached_res; 
     409        return MACHO_SUCCESS; 
     410    } 
     411 
    406412     
    407413    /* Open input file */ 
     
    430436        return MACHO_EMEM; 
    431437 
    432     int ret = parse_macho(*res, &input_file); 
     438    /* The output parameter *res should be read-only for the user of the lib only, but writable for 
     439     * us */ 
     440    int ret = parse_macho((macho_t *)*res, &input_file); 
    433441    if (ret == MACHO_SUCCESS) { 
    434         /* TODO: Insert into hashmap for caching */ 
    435         macho_t **handle_list = realloc(handle->mht_results, (handle->mht_result_count + 1) * sizeof(*handle->mht_results)); 
    436         if (handle_list == NULL) { 
    437             free_macho_t(*res); 
     442        /* Insert into hashmap for caching */ 
     443        if (0 == hashMapPut(handle->result_map, filepath, *res, NULL)) { 
     444            free_macho_t((macho_t *)*res); 
    438445            *res = NULL; 
    439446            ret = MACHO_EMEM; 
    440         } else { 
    441             handle->mht_results = handle_list; 
    442             handle->mht_result_count++; 
    443             handle->mht_results[handle->mht_result_count - 1] = *res; 
    444447        } 
    445448    } else { 
    446449        /* An error occured, free mt */ 
    447         free_macho_t(*res); 
     450        free_macho_t((macho_t *)*res); 
    448451        *res = NULL; 
    449452    } 
     
    461464    if (mht == NULL) 
    462465        return NULL; 
    463     memset(mht, 0, sizeof(mht)); 
     466    mht->result_map = hashMapCreate((void (*)(const void *))free_macho_t); 
     467    if (mht->result_map == NULL) { 
     468        free(mht); 
     469        return NULL; 
     470    } 
    464471    return mht; 
    465472} 
     
    470477        return; 
    471478     
    472     for (size_t i = 0; i < handle->mht_result_count; ++i) 
    473         free_macho_t(handle->mht_results[i]); 
    474     free(handle->mht_results); 
     479    hashMapDestroy(handle->result_map); 
    475480 
    476481    free(handle); 
  • branches/gsoc11-rev-upgrade/base/src/libmachista1.0/libmachista.h

    r80151 r80425  
    125125 * On error, the contents of res are undefined and should not be used. The memory associated with 
    126126 * the result *res will be free()'d and should thus not be used after calling macho_destroy_handle 
    127  * on the macho_handle_t used for the call. 
     127 * on the macho_handle_t used for the call. *res should also never be modified or otherwise 
     128 * free()'d. 
    128129 */ 
    129 int macho_parse_file(macho_handle_t *handle, const char *filepath, macho_t **res); 
     130int macho_parse_file(macho_handle_t *handle, const char *filepath, const macho_t **res); 
    130131 
    131132/** 
Note: See TracChangeset for help on using the changeset viewer.