/* I should really make a generic function to do this... */ static void *TagAlloc(unsigned long size) { void *tmp; tag_memory_usage += size; if(tag_memory_usage > TAG_MEMCAP) { /* aggressively prune */ struct timeval tv; struct timezone tz; tag_alloc_faults++; gettimeofday(&tv, &tz); if(!PruneTagCache((u_int32_t)tv.tv_sec, 0)) { /* if we can't prune due to time, just nuke 5 random sessions */ PruneTagCache(0, 5); } } tmp = (void *) calloc(size, sizeof(char)); if(tmp == NULL) { FatalError("TagAlloc(): Unable to allocate memory! (%d bytes in use)\n", tag_memory_usage); } return tmp; }
/** Allocate a TagNode * * Alocates a TagNode while guaranteeing that total memory usage remains within TAG_MEMCAP. * Least used nodes may be deleted from ssn_tag_cache and host_tag_cache to make space if * the limit is being exceeded. * * @param hash - pointer to SFXHASH that should point to either ssn_tag_cache_ptr * or host_tag_cache_ptr. * * @returns a pointer to new TagNode or NULL if memory couldn't * be allocated */ static TagNode * TagAlloc( SFXHASH *hash ) { TagNode *tag_node = NULL; if(tag_memory_usage + memory_per_node(hash) > TAG_MEMCAP) { /* aggressively prune */ struct timeval tv; struct timezone tz; int pruned_nodes = 0; tag_alloc_faults++; gettimeofday(&tv, &tz); pruned_nodes = PruneTagCache((uint32_t)tv.tv_sec, 0); if(pruned_nodes == 0) { /* if we can't prune due to time, just try to nuke * 5 not so recently used nodes */ pruned_nodes = PruneTagCache(0, 5); /* unlikely to happen since memcap has been reached */ if (pruned_nodes == 0) return NULL; } } tag_node = (TagNode *)calloc(1, sizeof(TagNode)); if (tag_node != NULL) tag_memory_usage += memory_per_node(hash); return tag_node; }