void cleanUp(Firm ** firm, Employee ** employees, Employee *** sortedEmployees) { Employee * employeesList = *employees; const int sortedListArrayListSize = listGetSize((*firm)->skills, sizeof(Skill)); /* Clean up the employeesList lists */ while(employeesList) { listClean((void **) &(employeesList->wishes), sizeof(Wish)); employeesList = employeesList->nextEmployee; } listClean((void **) employees, sizeof(Employee)); /* Clean sorted ArrayList of Employees */ int sortedEmployessCounter = 0; for(; sortedEmployessCounter < sortedListArrayListSize; sortedEmployessCounter++) { listClean((void **) *sortedEmployees + sortedEmployessCounter, sizeof(Skill)); } free(*sortedEmployees); *sortedEmployees = NULL; /* Clean firm */ listClean((void **) &((*firm)->emptySchedule), sizeof(Shift)); listClean((void **) &((*firm)->ranks), sizeof(Rank)); listClean((void **) &((*firm)->skills), sizeof(Skill)); free(*firm); *firm = NULL; }
bool isEmptyPQ(PQ *pq){ if(pq != NULL){ return listGetSize(pq->array) > 0; }else{ fprintf(stderr, "isEmptyPQ() - NULL is passed\n"); return false; } }
int graphCountEdges(Graph *G){ int total=0; GVertex *cvert; NodeList *p; for(p = listGetHeader(G); p!=NULL; p = nodeListGetNext(p)){ cvert = (GVertex*)nodeListGetCont(p); total += listGetSize(cvert->LAdjacents); } return total; }
/** * Sorts the list according to the given function, using a max sort sorting * method. * * For example, the following code will sort a list of integers according to * their distance from 0. * @code * int closerTo(ListElement num1, ListElement num2, ListSortKey value) { * int distance1 = abs(*(int*)num1 - *(int*)value); * int distance2 = abs(*(int*)num2 - *(int*)value); * return distance1 - distance2; * } * * void sortInts(List listOfInts) { * listSort(listOfInts, closerTo); * } * @endcode * * @param list the target list to sort * @param compareElement A comparison function as defined in the type * CompareListElements. This function should return an integer indicating the * relation between two elements in the list * * @return * LIST_NULL_ARGUMENT if list or compareElement are NULL * LIST_OUT_OF_MEMORY if a memory allocation failed, the list will be intact * in this case. * LIST_SUCCESS if sorting completed successfully. */ ListResult listSort( List list, CompareListElements compareElement ) { if ((list == NULL) || (compareElement == NULL)) return LIST_NULL_ARGUMENT; int list_size = listGetSize(list); if (list_size > 1) { for (int i = list_size - 1; list_size > 1; list_size--) { ListItem current = list->First; for (int j = 0; j < i; j++) { if (compareElement(current->Element, current->Next->Element) < 0) { ListElement element = current->Element; current->Element = current->Next->Element; current->Next->Element = element; } current = current->Next; } } } return LIST_SUCCESS; }
static int connectionMakeGetRequest(connectionContext_t* pCContext){ int count = listGetSize(pCContext->currentRequests); if (count < 1) { return 0; } request_t* pCurrent = listGetFirst(pCContext->currentRequests); int estimatedBufferSize = 0; while (pCurrent) { estimatedBufferSize = strlen(pCurrent->key) + 1; pCurrent = listGetNext(pCContext->currentRequests, pCurrent); } estimatedBufferSize -= 1; //don't need space after last key estimatedBufferSize += 4 + 2; // strlen("get ") + strlen("\r\n") u_int32_t offset = 0; char* buffer = 0; int written = 0; buffer = connectionGetBuffer(pCContext->connection, pCContext->fallocator, estimatedBufferSize, &offset); if (buffer) { request_t* pCurrent = listGetFirst(pCContext->currentRequests); strncpy(buffer+offset, "get ", 4); written += 4; while (pCurrent) { request_t* pNext = listGetNext(pCContext->currentRequests, pCurrent); int keyLength = strlen(pCurrent->key); strncpy(buffer+offset+written, pCurrent->key, keyLength); written+= keyLength; if (pNext) { strncpy(buffer+offset+written, " ", 1); written+= 1; } pCurrent = pNext; } strncpy(buffer+offset+written, "\r\n", 2); written += 2; LOG(DEBUG, "created request %.*s", written, buffer+offset); dataStreamAppendData(pCContext->writeStream, buffer, offset, written); //TODO : check return value } return written; }
static int connectionSubmitRequests(externalServer_t* pServer, connectionContext_t* pCContext) { //we have a free connection int count = MAX_MULTI_GET_REQUESTS; while (count-- > 0) { request_t* pRequest = listRemoveFirst(pServer->unassignedRequests); if (!pRequest) { break; } listAddLast(pCContext->currentRequests, pRequest); } if (listGetSize(pCContext->currentRequests) > 0) { listAddLast(pServer->activeConnections, pCContext); int bytesWritten = connectionMakeGetRequest(pCContext); if (bytesWritten > 0) { int err = completeWrite(pCContext); if (err == 0) { pCContext->status = status_waiting_read; connectionWaitForRead(pCContext->connection, getGlobalEventBase()); }else { if (err == 1) { pCContext->status = status_waiting_write; connectionWaitForWrite(pCContext->connection, getGlobalEventBase()); }else { // error writing to server/ connection closed ? listRemove(pServer->activeConnections, pCContext); connectionContextDelete(pCContext, 1); } } } }else { //pooled connections also wait for read, incase they are close by the server pCContext->status = status_pooled; listAddLast(pServer->freeConnections, pCContext); connectionWaitForRead(pCContext->connection, getGlobalEventBase()); } return 0; }
static int externalServerSubmit(externalServer_t* pServer, request_t* pRequest) { int returnValue = 0; connectionContext_t* pCContext = 0; connection_t newConnection = 0; listAddLast(pServer->unassignedRequests, pRequest); pCContext = listRemoveFirst(pServer->freeConnections); if (pCContext) { connectionWaitCancel(pCContext->connection, getGlobalEventBase()); } if (!pCContext) { //check if we have reached max concurrent connections limit // if not, create a new connection if (listGetSize(pServer->activeConnections) < MAX_CONCURRENT_CONNECTIONS) { LOG(DEBUG, "creating new external connection"); newConnection = connectionClientCreate(pServer->serverIP, pServer->serverPort, createConnectionHandler()); IfTrue(newConnection, ERR, "Error creating new connection to %s", pServer->serverName); //got a new connection pCContext = connectionContextCreate(newConnection, pServer); IfTrue(pCContext, ERR, "Error allocting memory for connection context"); connectionSetContext(newConnection, pCContext); newConnection = 0; int err = connectionConnect(pCContext->connection); IfTrue(err >= 0, ERR, "connect failed"); if (err == 1) { LOG(DEBUG, "waiting for connect to complete"); pCContext->status = status_connecting; connectionWaitForConnect(pCContext->connection, getGlobalEventBase()); goto OnSuccess; } }else { //if we have reached max connection limit, we will let the request rest //in the queue. Whenever one of the current connections get free, we will //use that to send the request. returnValue = 1; goto OnSuccess; } } if (pCContext) { pCContext->status = status_active; connectionSubmitRequests(pServer, pCContext); } goto OnSuccess; OnError: if (newConnection) { connectionClose(newConnection); } if (pCContext) { connectionContextDelete(pCContext, 0); } returnValue = 1; OnSuccess: return returnValue; }
static bool testClientsManagerGetSortedPayments(){ Email email = NULL, mail1 = NULL ,mail2 = NULL, mail3 = NULL; emailCreate("gaba@ganosh", &email); emailCreate("baba@gash", &mail1); emailCreate("baba@gash2", &mail2); emailCreate("baaa@gash", &mail3); ClientsManager manager = clientsManagerCreate(); clientsManagerAdd( manager, email, 1, 1, 200); clientsManagerAdd( manager, mail1, 1, 2, 1000); clientsManagerAdd( manager, mail2, 1, 2, 1000); clientsManagerAdd( manager, mail3, 1, 2, 1000); List clients_list = NULL; ASSERT_TEST( clientsManagerGetSortedPayments(NULL, &clients_list) == CLIENT_MANAGER_INVALID_PARAMETERS); ASSERT_TEST( clientsManagerGetSortedPayments(manager,NULL) == CLIENT_MANAGER_INVALID_PARAMETERS); ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) == CLIENT_MANAGER_SUCCESS); ASSERT_TEST( listGetSize(clients_list) == 0); listDestroy(clients_list); clientsManagerExecutePurchase( manager, email, 330); ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) == CLIENT_MANAGER_SUCCESS); ASSERT_TEST( listGetSize(clients_list) == 1); listDestroy(clients_list); clientsManagerExecutePurchase( manager, mail1, 900); ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) == CLIENT_MANAGER_SUCCESS); ASSERT_TEST( listGetSize(clients_list) == 2); ClientPurchaseBill top_bill = listGetFirst(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(top_bill), mail1)); ClientPurchaseBill second_bill = listGetNext(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(second_bill), email)); listDestroy(clients_list); clientsManagerExecutePurchase( manager, mail2, 900); ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) == CLIENT_MANAGER_SUCCESS); ASSERT_TEST( listGetSize(clients_list) == 3); top_bill = listGetFirst(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(top_bill), mail1)); second_bill = listGetNext(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(second_bill), mail2)); second_bill = listGetNext(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(second_bill), email)); listDestroy(clients_list); clientsManagerExecutePurchase( manager, mail3, 900); ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) == CLIENT_MANAGER_SUCCESS); ASSERT_TEST( listGetSize(clients_list) == 4); top_bill = listGetFirst(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(top_bill), mail3)); second_bill = listGetNext(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(second_bill), mail1)); second_bill = listGetNext(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(second_bill), mail2)); second_bill = listGetNext(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(second_bill), email)); listDestroy(clients_list); clientsManagerExecutePurchase(manager, email, 900); ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) == CLIENT_MANAGER_SUCCESS); ASSERT_TEST( listGetSize(clients_list) == 4); top_bill = listGetFirst(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(top_bill), email)); second_bill = listGetNext(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(second_bill), mail3)); second_bill = listGetNext(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(second_bill), mail1)); second_bill = listGetNext(clients_list); ASSERT_TEST( emailAreEqual( clientPurchaseBillGetClientEmail(second_bill), mail2)); listDestroy(clients_list); emailDestroy(email); emailDestroy(mail1); emailDestroy(mail2); emailDestroy(mail3); clientsManagerDestroy(manager); return true; }
int graphCountVertices(Graph *G){ return listGetSize(G); }