trick/trick_source/sim_services/Collect/collect.cpp
Alex Lin 19025d77ad Standardize directory names
Reorganized.  Created a new top level include directory that will hold all of Trick's header files. Moved all of the Trick headers to this directory.  Created a libexec directory that holds all of the executables that users don't need to execute directly.  Changed all of the executables remaining in bin to start with "trick-".  In the sim_services directories changed all source files to find the Trick headers in their new location.  Since all of the include files are gone in sim_services, removed the src directories as well, moving all of the source files up a level.  Moved the makefiles, docs, man, and other architecture independent files into a top level share directory.  Renamed lib_${TRICK_HOST_CPU} to lib64 or lib depending on the platform we're currently on.

refs #63
2015-06-09 08:44:42 -05:00

102 lines
2.6 KiB
C++

#include <stdlib.h>
#include <string.h>
#include "trick/collect_macros.h"
/**
@brief
Adds the addess, collectee, to the list pointed to by collector. Returns a new pointer
to the new list. The old list a collector will be freed.
@details
-# Get the current list length
-# Allocate enough space to accomodate the current list + 1. (+1 for the size of the list)
-# Set the size of the new list in the first slot of the new list.
-# Copy the old list to the new.
-# Append the new item to the new list.
-# Delete the old list
-# Return the new list
*/
void ** add_collect( void ** collector , void * collectee ) {
int collect_size = NUM_COLLECT(collector) ;
void ** new_list ;
long * lp ;
// create a new list
new_list = (void**)malloc((collect_size + 2) * sizeof(void*));
// set the list size
lp = ( long *)new_list ;
*lp = collect_size + 1 ;
// copy old list
new_list++ ;
if ( collect_size > 0 ) {
memcpy( new_list , collector , collect_size * sizeof(void *)) ;
}
new_list[collect_size] = collectee ;
// remove old list
if ( collector ) {
free(collector - 1) ;
}
return new_list ;
}
/**
@brief
Deletes the addess, collectee, from the list pointed to by collector. Returns a new pointer
to the new list. The old list a collector will be freed.
@details
-# Get the current list length
-# Count the number of items in the list that do not match the collectee.
-# Allocate enough space to accomodate the new list. (+1 for the size of the list)
-# Set the size of the new list in the first slot of the new list.
-# Copy all of the items that do not match collectee to the new list
-# Delete the old list
-# Return the new list
*/
void ** delete_collect( void ** collector , void * collectee ) {
int collect_size = NUM_COLLECT(collector) ;
int new_count = 0 ;
void ** new_list ;
long * lp ;
int ii , jj ;
for ( ii = 0 ; ii < collect_size ; ii++ ) {
if ( collector[ii] != collectee ) {
new_count++ ;
}
}
if ( new_count > 0 ) {
// create a new list
new_list = (void**)malloc((new_count + 1) * sizeof(void*));
// set the list size
lp = ( long *)new_list ;
*lp = new_count ;
// copy old list
new_list++ ;
jj = 0 ;
for ( ii = 0 ; ii < collect_size ; ii++ ) {
if ( collector[ii] != collectee ) {
new_list[jj++] = collector[ii] ;
}
}
} else {
new_list = NULL ;
}
// remove old list
if ( collector ) {
free(collector - 1) ;
}
return new_list ;
}