extern int std_create_event(std_event_id_t *pEventId) { std_posix_event_t *eventId; eventId = (std_posix_event_t *) std_malloc(sizeof(std_posix_event_t)); if (eventId == NULL) /* Failed to allocate memory */ return 0; if (pthread_mutex_init(&eventId->mutex, NULL) != 0) { /* Failed to init mutex */ std_free(eventId); return 0; } if (pthread_cond_init(&eventId->condition, NULL) != 0) { /* Failed to init condition */ pthread_mutex_destroy(&eventId->mutex); std_free(eventId); return 0; } *pEventId = eventId; return 1; }
extern int std_create_semaphore(std_semaphore_id_t *pSemId) { std_posix_sem_t *semId; semId = (std_posix_sem_t *) std_malloc(sizeof(std_posix_sem_t)); if (semId == NULL) /* Failed to allocate memory */ return 0; if (pthread_mutex_init(&semId->mutex, NULL) != 0) { /* Failed to init mutex */ std_free(semId); return 0; } if (pthread_cond_init(&semId->condition, NULL) != 0) { /* Failed to init condition */ pthread_mutex_destroy(&semId->mutex); std_free(semId); return 0; } /* Initialize count */ semId->count = 0; *pSemId = (std_semaphore_id_t) semId; return 1; }
extern int std_delete_semaphore(std_semaphore_id_t semId) { if (semId == STD_NO_SEMAPHORE) return 0; std_free((void *) semId); return 1; }
extern int std_delete_event(std_event_id_t eventId) { if (eventId == STD_NO_EVENT) /* Return failed for operating empty object */ return 0; pthread_mutex_destroy(&eventId->mutex); pthread_cond_destroy(&eventId->condition); std_free(eventId); return 1; }
extern int std_delete_semaphore(std_semaphore_id_t semId) { if (semId == STD_NO_SEMAPHORE) /* Return failed for operating empty object */ return 0; pthread_mutex_destroy(&semId->mutex); pthread_cond_destroy(&semId->condition); std_free(semId); return 1; }
/* Free all tasks' name */ extern void std_cleanup_all_tasks_name() { std_get_spin_lock(&std_tin_spin_lock); /* Free map */ std_free(std_task_id_name_map); std_task_id_name_map = NULL; std_task_id_name_size = 0; std_task_id_name_max_size = 0; std_release_spin_lock(&std_tin_spin_lock); }
extern int std_delete_mutex(std_mutex_id_t mutexId) { int rc; if (mutexId == STD_NO_MUTEX) /* Return failed for operating empty object */ return 0; if ((rc = pthread_mutex_destroy(mutexId)) != 0 && rc != EBUSY) { /* Failed to delete a mutex */ return 0; } std_free(mutexId); return 1; }
static void StdCacheDelete(MemoryContext context) { StdHashEntry *she; DBG("Enter: StdCacheDelete"); /* lookup the hash entry in the global hash table so we can free it */ she = GetStdHashEntry(context); if (!she) elog(ERROR, "StdCacheDelete: Trying to delete non-existant hash entry object with MemoryContext key (%p)", (void *)context); DBG("deleting std object (%p) with MemoryContext key (%p)", she->std, context); if (she->std) std_free(she->std); DeleteStdHashEntry(context); }
/* Entry of os thread */ extern void *std_task_entry(std_task_para_t *taskPara) { void *entry; void *para; void *ret; std_set_task_name(std_get_current_task_id(), taskPara->name); /* Save to stack */ entry = taskPara->entry; para = taskPara->para; /* Drop input parameter */ std_free(taskPara); /* Enter */ ret = ((void *(*)(void *)) entry)(para); /* Free name when thread exit */ std_delete_task_name(std_get_current_task_id()); return ret; }
void inet_iface_destroy(inet_iface_t iface){ std_free(iface -> in_addr_str); std_free(iface); }
extern int std_unmap_shrd_mem(std_shrd_mem_id_t memId, void *pMapAt, size_t size) { std_free(pMapAt); return 1; }
static STANDARDIZER * CreateStd(char *lextab, char *gaztab, char *rultab) { STANDARDIZER *std; LEXICON *lex; LEXICON *gaz; RULES *rules; int err; int SPIcode; DBG("Enter: CreateStd"); SPIcode = SPI_connect(); if (SPIcode != SPI_OK_CONNECT) { elog(ERROR, "CreateStd: couldn't open a connection to SPI"); } std = std_init(); if (!std) elog(ERROR, "CreateStd: could not allocate memory (std)"); lex = lex_init(std->err_p); if (!lex) { std_free(std); SPI_finish(); elog(ERROR, "CreateStd: could not allocate memory (lex)"); } err = load_lex(lex, lextab); if (err == -1) { lex_free(lex); std_free(std); SPI_finish(); elog(ERROR, "CreateStd: failed to load '%s' for lexicon", lextab); } gaz = lex_init(std->err_p); if (!gaz) { lex_free(lex); std_free(std); SPI_finish(); elog(ERROR, "CreateStd: could not allocate memory (gaz)"); } err = load_lex(gaz, gaztab); if (err == -1) { lex_free(gaz); lex_free(lex); std_free(std); SPI_finish(); elog(ERROR, "CreateStd: failed to load '%s' for gazeteer", gaztab); } rules = rules_init(std->err_p); if (!rules) { lex_free(gaz); lex_free(lex); std_free(std); SPI_finish(); elog(ERROR, "CreateStd: could not allocate memory (rules)"); } err = load_rules(rules, rultab); if (err == -1) { rules_free(rules); lex_free(gaz); lex_free(lex); std_free(std); SPI_finish(); elog(ERROR, "CreateStd: failed to load '%s' for rules", rultab); } std_use_lex(std, lex); std_use_gaz(std, gaz); std_use_rules(std, rules); std_ready_standardizer(std); SPI_finish(); return std; }
int main(int argc, char *argv[]) { STANDARDIZER *std; LEXICON *lex; LEXICON *gaz; RULES *rules; char buf[1024]; int seq; char input_str[ 4096 ] ; char word[512]; char stdword[512]; int token; int nr; int rule[RULESIZE]; int err; int cnt; int option = 0; FILE *in; if (argc == 3 && !strcmp(argv[1], "-o")) { option = strtol(argv[2], NULL, 10); argc -= 2; argv += 2; } else if (argc != 1) Usage(); std = std_init(); assert(std); lex = lex_init(std->err_p); assert(lex); in = fopen(LEXIN, "rb"); assert(in); cnt = 0; while (!feof(in) && fgets(buf, 1024, in)) { cnt++; /* parse into fields */ if (parse_csv(buf, &seq, word, stdword, &token)) { /* add the record to the lexicon */ err = lex_add_entry(lex, seq, word, stdword, token); if (err != 1) printf("lex: Failed: %d: %s", cnt, buf); } else { printf("lex: Skipping: %d: %s", cnt, buf); } } fclose(in); if (option & 1) { printf("------------ address lexicon --------------\n"); print_lexicon(lex->hash_table); printf("\n"); } gaz = lex_init(std->err_p); assert(gaz); in = fopen(GAZIN, "rb"); assert(in); cnt = 0; while (!feof(in) && fgets(buf, 1024, in)) { cnt++; /* parse into fields */ if (parse_csv(buf, &seq, word, stdword, &token)) { /* add the record to the lexicon */ err = lex_add_entry(gaz, seq, word, stdword, token); if (err != 1) printf("gaz: Failed: %d: %s", cnt, buf); } else { printf("gaz: Skipping: %d: %s", cnt, buf); } } fclose(in); if (option & 2) { printf("------------ gazeteer lexicon --------------\n"); print_lexicon(gaz->hash_table); printf("\n"); } rules = rules_init(std->err_p); assert(rules); rules -> r_p -> collect_statistics = TRUE ; /* ************ RULES **************** */ in = fopen(RULESIN, "rb"); assert(in); cnt = 0; while (!feof(in) && fgets(buf, 1024, in)) { cnt++; /* parse into fields */ nr = parse_rule(buf, rule); /* add the record to the rules */ err = rules_add_rule(rules, nr, rule); if (err != 0) printf("rules: Failed: %d (%d): %s", cnt, err, buf); } err = rules_ready(rules); if (err != 0) printf("rules: Failed: err=%d\n", err); fclose(in); std_use_lex(std, lex); std_use_gaz(std, gaz); std_use_rules(std, rules); std_ready_standardizer(std); printf( "Standardization test. Type \"exit\" to quit:\n" ) ; fflush( stdout ) ; while ( TRUE ) { err = standardize_command_line( std, input_str, option ) ; if ( err == FAIL ) { break ; } } printf( "OK\n" ) ; fflush( stdout ) ; std_free(std); /* these were freed when we bound them with std_use_*() rules_free(rules); lex_free(gaz); lex_free(lex); */ return 0; }
/* Set task id -> name */ extern int std_set_task_name(std_task_id_t taskId, const char *name) { int index; int ret = 0; std_get_spin_lock(&std_tin_spin_lock); do { /* Get index to set/insert new name */ index = std_find_task_id(taskId); STD_ASSERT(index >= 0 && index <= std_task_id_name_size); if (index < std_task_id_name_size && std_task_id_name_map[index].taskId == taskId) { /* Replace previous one */ strncpy(std_task_id_name_map[index].taskName, name, STD_MAX_tASK_NAME_LEN); break; } /* Insert @ index */ if (std_task_id_name_size >= std_task_id_name_max_size) { std_task_id_name_t *new_name_map; int new_size; /* Expand map */ new_size = std_task_id_name_max_size * 2; if (new_size < 16) new_size = 16; new_name_map = std_malloc(sizeof(std_task_id_name_t) * new_size); if (new_name_map == NULL) { /* Failed to allocate memory to add task name */ break; } /* Free old & replace with new one */ if (std_task_id_name_map != NULL) { memcpy(new_name_map, std_task_id_name_map, sizeof(std_task_id_name_t) * std_task_id_name_max_size); std_free(std_task_id_name_map); } std_task_id_name_map = new_name_map; std_task_id_name_max_size = new_size; } /* Put item @ index */ memmove(&std_task_id_name_map[index + 1], &std_task_id_name_map[index], sizeof(std_task_id_name_t) * (std_task_id_name_size - index)); std_task_id_name_map[index].taskId = taskId; strncpy(std_task_id_name_map[index].taskName, name, STD_MAX_tASK_NAME_LEN); std_task_id_name_size++; ret = 1; } while (0); std_release_spin_lock(&std_tin_spin_lock); return ret; }