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_create_semaphore(std_semaphore_id_t *pSemId) { *pSemId = (std_semaphore_id_t) std_malloc(sizeof(int)); if (*pSemId == NULL) return 0; return 1; }
inet_iface_t inet_iface_create_by_str(const char* in_addr_str){ inet_iface_t iface = (inet_iface_t)std_malloc(sizeof(inet_iface)); iface -> in_addr_str = strdup(in_addr_str); std_inet_aton(iface -> in_addr_str, &iface -> in_addr); return iface; }
inet_iface_t inet_iface_create(const struct in_addr *addr){ char *addr_str; inet_iface_t iface = (inet_iface_t)std_malloc(sizeof(inet_iface)); std_memcpy(&iface -> in_addr, addr, sizeof(struct in_addr)); addr_str = std_inet_ntoa(*addr); iface -> in_addr_str = strdup(addr_str); return iface; }
/* Allocate & create task para for task entry */ extern std_task_para_t *std_create_task_para(const char *name, void *entry, void *para) { std_task_para_t *taskPara; taskPara = std_malloc(sizeof(std_task_para_t)); if (taskPara == NULL) /* Can't not create task para */ return NULL; memset(taskPara, 0, sizeof(std_task_para_t)); if (name != NULL) { strncpy(taskPara->name, name, sizeof(taskPara->name)); taskPara->name[sizeof(taskPara->name) - 1] = 0; } taskPara->entry = entry; taskPara->para = para; return taskPara; }
extern int std_create_mutex(std_mutex_id_t *pMutexId) { std_mutex_id_t mutexId; pthread_mutexattr_t attr; int rc; mutexId = (std_mutex_id_t) std_malloc(sizeof(pthread_mutex_t)); if (mutexId == NULL) return 0; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); rc = pthread_mutex_init(mutexId, &attr); pthread_mutexattr_destroy(&attr); if (rc != 0) /* Failed to init mutex */ return 0; *pMutexId = mutexId; return 1; }
extern int std_map_shrd_mem(std_shrd_mem_id_t memId, size_t size, void **ppMapAt) { *ppMapAt = std_malloc(size); return 1; }
/* 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; }