Ejemplo n.º 1
0
static bool callback(void *key, void *value, void *context) {
    struct image_renderers *ir = (struct image_renderers *) context;
    hashmapRemove(ir->images, key);
    if (value)
        gdImageDestroy((gdImagePtr) value);
    return 1;
}
Ejemplo n.º 2
0
/**
 * Called when the peer dies.
 */
static void peerProxyKill(PeerProxy* peerProxy, bool errnoIsSet) {
    if (errnoIsSet) {
        ALOGI("Peer %d died. errno: %s", peerProxy->credentials.pid, 
                strerror(errno));
    } else {
        ALOGI("Peer %d died.", peerProxy->credentials.pid);
    }
    
    // If we lost the master, we're up a creek. We can't let this happen.
    if (peerProxy->master) {    
        LOG_ALWAYS_FATAL("Lost connection to master.");
    }

    Peer* localPeer = peerProxy->peer;
    pid_t pid = peerProxy->credentials.pid;
    
    peerLock(localPeer);
    
    // Remember for awhile that the peer died.
    localPeer->deadPeers[localPeer->deadPeerCursor] 
        = peerProxy->credentials.pid;
    localPeer->deadPeerCursor++;
    if (localPeer->deadPeerCursor == PEER_HISTORY) {
        localPeer->deadPeerCursor = 0;
    }
  
    // Remove from peer map.
    hashmapRemove(localPeer->peerProxies, &pid);
    
    // External threads can no longer get to this peer proxy, so we don't 
    // need the lock anymore.
    peerUnlock(localPeer);
    
    // Remove the fd from the selector.
    if (peerProxy->fd != NULL) {
        peerProxy->fd->remove = true;
    }

    // Clear outgoing packet queue.
    while (peerProxyNextPacket(peerProxy)) {}

    bufferFree(peerProxy->inputBuffer);

    // This only applies to the master.
    if (peerProxy->connections != NULL) {
        // We can't leave these other maps pointing to freed memory.
        hashmapForEach(peerProxy->connections, &peerProxyRemoveConnection, 
                peerProxy);
        hashmapFree(peerProxy->connections);
    }

    // Invoke death listener.
    localPeer->onDeath(pid);

    // Free the peer proxy itself.
    free(peerProxy);
}
Ejemplo n.º 3
0
void graphRemoveGLI(graph *ctx, size_t ID) {
    //Unregister this GLI;
    GLI *gli = (GLI*) hashmapRemove(ctx->GIDMap, ID);
    fastlistRemoveByPointer(ctx->nodes, gli);

    // Get Connection lists
    fastlist *srcList = (fastlist*) hashmapGet(ctx->srcMap, ID);
    fastlist *drnList = (fastlist*) hashmapGet(ctx->drnMap, ID);

    // Remove src connections here.
    {
        size_t count = fastlistSize(srcList);
        for (size_t i = 0; i < count; i++) {
            // Get the connection.
            connection *conn = (connection *) fastlistGetIndex(srcList, i);
            size_t index = conn->ID;
            // Remove it
            graphRemoveConnection(ctx, index);
        }
    }

    // Remove drn connections here.
    {
        size_t count = fastlistSize(drnList);
        for (size_t i = 0; i < count; i++) {
            // Get the connection.
            connection *conn = (connection *) fastlistGetIndex(drnList, i);
            size_t index = conn->ID;
            // Remove it
            graphRemoveConnection(ctx, index);
        }
    }

    // Free the connection lists.
    hashmapRemove(ctx->srcMap, ID);
    hashmapRemove(ctx->drnMap, ID);
    fastlistDelete(srcList);
    fastlistDelete(drnList);

    // Done, cleanup

    free(gli);
    ctx->nodeCount--;
};
Ejemplo n.º 4
0
//TODO DELETE NODE carefully  some other threads can use this entitty.
int registryDeleteNode(Registry registry,Node node){	
	RegistryInternal* internal=(RegistryInternal*)registry;
	NodeInternal* nodeToRemove=(NodeInternal*)node;
        // Free the attributes.
        registryRemoveAttributes(registry,node,nodeToRemove->attributes);
	// Remove this item from the registry map;
	hashmapRemove(internal->nodes,nodeToRemove->id);
        stringHashmapFree(nodeToRemove->children);
        stringHashmapFree(nodeToRemove->attributes);
        return VALID;
}
Ejemplo n.º 5
0
extern void	MIOHashmap_Remove (OOTint pmMapID, OOTstring key)
{
	hashmap *myMap = (hashmap*)MIO_IDGet (pmMapID, HASHMAP_ID);
	OOTint *retrieved;

	retrieved = (OOTint*)hashmapRemove (myMap,key);

	if(retrieved != NULL) {
		free(retrieved);
	}
	
}
Ejemplo n.º 6
0
int tt_parse_ttdelete(struct CIFParser *parser) {
    struct TimeTable *p = (struct TimeTable *) parser;

    char tiploc[8];
    cif_readString(parser->buf, 2, tiploc, 7);
    struct TTTiploc *tpl = (struct TTTiploc *) hashmapRemove(p->loc, tiploc);
    if (tpl) {
        free(tpl);
    } else {
        logconsole("WARNING: Tiploc '%s' to delete but not found", tiploc);
    }

    return EXIT_SUCCESS;
}
Ejemplo n.º 7
0
bool handle_domain(void *k, void *l, void *c)
{
    lookup_t *lookup = (lookup_t *) l;
    lookup_context_t *context = (lookup_context_t *) c;
    struct timeval now;
    gettimeofday(&now, NULL);
    if (timediff(&now, &lookup->next_lookup) < 0)
    {
        uint16_t query_flags = 0;
        if (!context->cmd_args.norecurse)
        {
            query_flags |= LDNS_RD;
        }
        ldns_pkt *packet;
        if(LDNS_STATUS_OK != ldns_pkt_query_new_frm_str(&packet, lookup->domain, context->cmd_args.record_types, LDNS_RR_CLASS_IN,
                                   query_flags))
        {
            abort();
        }
        ldns_pkt_set_id(packet, lookup->transaction);
        uint8_t *buf = NULL;
        size_t packet_size = 0;
        if(LDNS_STATUS_OK != ldns_pkt2wire(&buf, packet, &packet_size))
        {
            abort();
        }
        ldns_pkt_free(packet);
        packet = NULL;
        sockaddr_in_t *resolver = massdns_get_resolver((size_t) rand(), &context->resolvers);
        ssize_t n = -1;
        while (n < 0)
        {
            n = sendto(context->sock, buf, packet_size, 0, (sockaddr_t *) resolver, sizeof(*resolver));
        }
        free(buf);
        long addusec = context->cmd_args.interval_ms * 1000;
        addusec += rand() % (addusec / 5); // Avoid congestion by adding some randomness
        lookup->next_lookup.tv_usec = (now.tv_usec + addusec) % 1000000;
        lookup->next_lookup.tv_sec = now.tv_sec + (now.tv_usec + addusec) / 1000000;
        lookup->tries++;
        if (lookup->tries == context->cmd_args.resolve_count)
        {
            hashmapRemove(context->map, lookup->domain);
            free(lookup->domain);
            free(lookup);
        }
    }
    return true;
}
Ejemplo n.º 8
0
void commandContainerCleanup(CommandContainer commandContainer){
	CommandContainerInternal* internal=(CommandContainerInternal*)commandContainer;
        lockLock(internal->lock);
        time_t currentTime;       // Stores seconds elapsed since 01-01-1970
        time(&currentTime);
        // Cleanup atomic commands
        if(linkedListSize(internal->atomicCommands)>0){
            int length=linkedListSize(internal->atomicCommands);
            Command atomicCommand;
            while(length>0){
                 linkedListGetFirst(internal->atomicCommands,(Object*)&atomicCommand);
                 int commandTime=commandGetInt(atomicCommand,SERVER_HANDLING_TIME);
                 int distributed=commandGetInt(atomicCommand,DISTRIBUTED);
                 if((currentTime-commandTime>MAX_TIME_TO_HOLD_COMMAND&&distributed==TRUE)||(currentTime-commandTime>MAX_TIME_TO_HOLD_UNCLEANED_COMMAND)){
                   commandFree(atomicCommand);
                   linkedListRemoveFirst(internal->atomicCommands);
                 }else{
                     break;
                 }
                 length=linkedListSize(internal->atomicCommands);
            }
        }
        
        // Cleanup commands
        int length=linkedListSize(internal->versions);
        int i;
        int* intBuffer;
        while(length>0){
            linkedListGetFirst(internal->versions,(Object*)&intBuffer);
            Command temp;
            hashmapGet(internal->versionMap,*intBuffer,(Object*)&temp);
            int commandTime=commandGetInt(temp,SERVER_HANDLING_TIME);            
            int persisted=commandGetInt(temp,PERSISTED);
            int distributed=commandGetInt(temp,DISTRIBUTED);
            if((currentTime-commandTime>MAX_TIME_TO_HOLD_COMMAND&&persisted==TRUE&&distributed==TRUE)||(currentTime-commandTime>MAX_TIME_TO_HOLD_UNCLEANED_COMMAND)){
                linkedListRemoveFirst(internal->versions);
                hashmapRemove(internal->versionMap,*intBuffer);
                memoryFree(intBuffer);
                commandFree(temp);
            }else{
                break;
            }
            length=linkedListSize(internal->versions);
        }
        lockUnlock(internal->lock);
        
        

}
Ejemplo n.º 9
0
void commandContainerRevertCommand(CommandContainer commandContainer,int lastVersion){
    CommandContainerInternal* internal=(CommandContainerInternal*)commandContainer;
    lockLock(internal->lock);
    int* intBuffer=NULL;
    linkedListGetLast(internal->versions,(Object*)&intBuffer);
    while(intBuffer!=NULL&&lastVersion<=*intBuffer){
        linkedListRemoveLast(internal->versions);
        Command temp;
        hashmapGet(internal->versionMap,*intBuffer,(Object*)&temp);
        hashmapRemove(internal->versionMap,*intBuffer);
        linkedListGetLast(internal->versions,(Object*)&intBuffer);
    }
    lockUnlock(internal->lock);
   
}
Ejemplo n.º 10
0
void graphRemoveConnection(graph *ctx, size_t ID) {
    // Remove Connection from hashmap
    connection *conn = (connection*) hashmapRemove(ctx->CIDMap, ID);

    // Remove connection from Source and drain map/lists
    fastlist *drnList = (fastlist*) hashmapGet(ctx->srcMap, conn->srcID);
    fastlistRemoveByPointer(drnList, (void*)conn);
    fastlist *srcList = (fastlist*) hashmapGet(ctx->drnMap, conn->drnID);
    fastlistRemoveByPointer(srcList, (void*)conn);

    // Remove the Connection from main list
    fastlistRemoveByPointer(ctx->connections, conn);

    // Finally free the Connection
    free(conn);
}
Ejemplo n.º 11
0
int hashmap_test(void) {
	printf("Starting hashmap tests...\n\n");

	hashmap* hmap = hashmapCreate(20);
	if (hashmapCount(hmap) != 0){
		printf("\nTest failed. Hashmap does not initialize to empty.");
		return -1;
	}

	hashmapInsert(hmap, 1, 1);
	if (hashmapCount(hmap) != 1){
		printf("\nTest failed. Hashmap does not have proper count of elements.");
		return -1;
	}
	int test = 0;

	test = (int *) hashmapGet(hmap, 1);

	if (test != 1){
		printf("\nTest failed. Value found inside was actually %d ", test);
		return -1;
	}
	hashmapInsert(hmap, "Word", 2);

	char* word;

	word = (char*) hashmapGet(hmap, 2);
	if (strcmp(word, "Word") != 0){
		printf("\nTest failed. The word found inside was actually %s", word);
		return -1;
	}

	test = (int*) hashmapRemove(hmap, 1);

	printf("\nTest 5: ");
	if (test == -1){
		printf("\nTest failed. HASHMAPERROR returned.");
		return -1;
	}
	if (hashmapCount(hmap) != 1){
		printf("\nTest failed. Hashmap value not actually removed");
		return -1;
	}


	printf("\nTest 6: ");
	hashmapInsert(hmap, 2, 1);
	test = (int *) hashmapGet(hmap, 1);
	if (test != 2){
		printf("\nTest failed. Value was not inserted.");
		return -1;
	}

	hashmapInsert(hmap, 7, 1);
	test = (int *) hashmapGet(hmap, 1);
	if (test != 7){
		printf("\nTest failed. Value was not replaced.");
		return -1;
	}

	hashmapInsert(hmap, 'd', 13);
	char c = (char) hashmapGet(hmap, 13);
	if (c != 'd'){
		printf("\nTest failed. Char 'd' was not properly inserted");
		return -1;
	}

	hashmapInsert(hmap, 9, 4294967295);
	test = (int *) hashmapGet(hmap, 4294967295);
	if (test != 9){
		printf("\nTest failed. 9 was not inserted");
		return -1;
	}

	test = hashmapRemove(hmap, 11);

	if (test != -1){
		printf("\nTest failed. ERROR should be returned.");
		return -1;
	}

	test = hashmapGet(hmap, 11);

	if (test != -1){
		printf("\nTest failed. ERROR should be returned.");
		return -1;
	}
	hashmapDelete(hmap);

	hashmap* hmap2 = hashmapCreate(10);
	int x = 0;
	while(x < 200){
		hashmapInsert(hmap2, x, x);
		int test2 = (int*) hashmapGet(hmap, x);
		if (test2 != x){
			printf("\nTest failed. Improper value retrieved.");
			return -1;
		}
		x++;
	}
	x = 0;
	while (x< 200){
		hashmapGet(hmap, 1);
		int test2 = (int*) hashmapGet(hmap, 1);
		if (test2 != 1){
			printf("\nTest failed. Constant retrieval does not work.");
			return -1;
		}
		x++;
	}
	x = 0;
	while (x < 200){
		int test2 = (int*) hashmapRemove(hmap, x);
		if (test2 == -1){
			printf("\nTest failed. ERROR was thrown, when it shouldn't have been.");
			return -1;
		}
		x++;
	}
	printf("\n\nAll hashmap tests passed!\n");
	hashmapDelete(hmap2);
	return 0;
}
Ejemplo n.º 12
0
void imagerenderer_removeImage(struct image_renderers *ir, const char *name) {
    gdImagePtr existing = (gdImagePtr) hashmapRemove(ir->images, (void *) name);
    if (existing)
        gdImageDestroy(existing);
}
Ejemplo n.º 13
0
int move(int x1,
         int y1,
         int x2,
         int y2) {
    /// Validates fields.
    if (!isValidField(currentGame.mapSize, x1, y1) ||
        !isValidField(currentGame.mapSize, x2, y2)) {
        return ERROR;
    }

    /// Validates initialization.
    if (currentGame.isInitialized == false) {
        return ERROR;
    }

    /// Validates distance between fields.
    if (distMax(x1, y1, x2, y2) != 1) {
        return ERROR;
    }

    /// Picks pawn from board.
    pawn *currentPawn = hashmapRemove(currentGame.gameMap, x1, y1);

    /// Checks if any pawn was picked.
    if (currentPawn == NULL) {
        return ERROR;
    }

    /// Checks if pawn is able to move.
    if (currentPawn->lastMove >= currentGame.currentRound) {
        free(currentPawn);
        return ERROR;
    }

    /// Checks if pawn belong to current player.
    if (currentGame.playerTurn != getPawnAdherence(currentPawn)) {
        free(currentPawn);
        return ERROR;
    }

    currentPawn->lastMove = currentGame.currentRound;

    pawn *targetPawn = hashmapRemove(currentGame.gameMap, x2, y2);

    if (getPawnAdherence(currentPawn) == getPawnAdherence(targetPawn)) {
        ///< Player wants to move onto his own pawn.
        free(currentPawn);
        if (targetPawn != NULL) {
            free(targetPawn);
        }
        return ERROR;
    } else {
        int actionResult = performAction(currentPawn, targetPawn);
        switch (actionResult) {
            /// Frees pawn that was killed.
            /// Puts pawn that survived.
            case UNIT_MOVED:
                currentPawn->x = (unsigned int) x2 - 1; ///< 1-based to 0-based.
                currentPawn->y = (unsigned int) y2 - 1;
                hashmapPut(currentGame.gameMap, currentPawn);
                break;
            case ATTACKER_KILLED:
                currentPawn->x = (unsigned int) x2 - 1;
                currentPawn->y = (unsigned int) y2 - 1;
                hashmapPut(currentGame.gameMap, currentPawn);
                free(targetPawn);
                break;
            case DEFENDER_KILLED:
                hashmapPut(currentGame.gameMap, targetPawn);
                free(currentPawn);
                break;
            case ATTACKER_KILLED_KING:
                currentPawn->x = (unsigned int) x2 - 1;
                currentPawn->y = (unsigned int) y2 - 1;
                hashmapPut(currentGame.gameMap, currentPawn);
                free(targetPawn);
                /// Player that made move is victorious.
                return currentGame.playerTurn == PLAYER_A_TURN ? PLAYER_A_WON : PLAYER_B_WON;
            case DEFENDER_KILLED_KING:
                free(currentPawn);
                hashmapPut(currentGame.gameMap, targetPawn);
                /// Player that made move is beaten.
                return currentGame.playerTurn == PLAYER_A_TURN ? PLAYER_B_WON : PLAYER_A_WON;
            case BOTH_UNITS_DIED:
                if (isKing(currentPawn)) {
                    free(currentPawn);
                    free(targetPawn);
                    return DRAW; ///< Kings killed each other.
                } else {
                    free(currentPawn);
                    free(targetPawn);
                }
                break;
            default:
                return ERROR;
        }
    }

    return GAME_OK;
}
Ejemplo n.º 14
0
/**
 * Cleans up connection information.
 */
static bool peerProxyRemoveConnection(void* key, void* value, void* context) {
    PeerProxy* deadPeer = (PeerProxy*) context;
    PeerProxy* otherPeer = (PeerProxy*) value;
    hashmapRemove(otherPeer->connections, &(deadPeer->credentials.pid));
    return true;
}
Ejemplo n.º 15
0
void massdns_handle_packet(ldns_pkt *packet, struct sockaddr_storage ns, void *ctx)
{
    if (!packet || ldns_pkt_qdcount(packet) != 1)
    {
        return;
    }
    struct timeval now;
    gettimeofday(&now, NULL);
    lookup_context_t *context = (lookup_context_t *) ctx;
    ldns_pkt_rcode response_code = ldns_pkt_get_rcode(packet);
    ldns_rr_list l = ldns_pkt_question(packet)[0];
    ldns_rr *question = ldns_rr_list_rr(&l, 0);
    ldns_rdf* owner = ldns_rr_owner(question);
    char* name = ldns_rdf2str(owner);
    size_t name_len = strlen(name);
    if(name_len > 0 && name[name_len - 1] == '.')
    {
        name[name_len - 1] = 0;
    }
    lookup_t *lookup = hashmapGet(context->map, name);
    free(name);
    if (lookup == NULL)
    {
        return;
    }
    if (response_code == LDNS_RCODE_NOERROR || response_code == LDNS_RCODE_NXDOMAIN ||
        lookup->tries == context->cmd_args.resolve_count)
    {
        switch (response_code)
        {
            case LDNS_RCODE_NOERROR:
                stats.noerr++;
                break;
            case LDNS_RCODE_FORMERR:
                stats.formerr++;
                break;
            case LDNS_RCODE_SERVFAIL:
                stats.servfail++;
                break;
            case LDNS_RCODE_NXDOMAIN:
                stats.nxdomain++;
                break;
            case LDNS_RCODE_NOTIMPL:
                stats.notimp++;
                break;
            case LDNS_RCODE_REFUSED:
                stats.refused++;
                break;
            default:
                stats.other++;
                break;
        }
        context->current_rate++;
        ldns_buffer *buf = ldns_buffer_new(LDNS_MAX_PACKETLEN);
        if(buf == NULL)
        {
            abort();
        }
        if(LDNS_STATUS_OK != output_packet(buf, packet, ns, context))
        {
            abort();
        }
        char* packetstr = ldns_buffer_export2str(buf);
        if(packetstr == NULL)
        {
            abort();
        }
        fprintf(stdout, "%s", packetstr);
        free(packetstr);
        if (timediff(&now, &context->next_update) <= 0)
        {
            print_stats(context);
        }
        ldns_buffer_free(buf);
        hashmapRemove(context->map, lookup->domain);
        free(lookup->domain);
        free(lookup);
    }
}
Ejemplo n.º 16
0
/*
 * Handler to handle both TI (Insert) and TU (update) records.
 * 
 * These records are identical except for update has possible additional entry for renaming a tiploc
 */
int tt_parse_ttinsert(struct CIFParser *parser) {
    struct TimeTable *p = (struct TimeTable *) parser;

    int insert = 0;

    // Lookup for existing entry (update) or insert)
    char tiploc[8];
    cif_readString(parser->buf, 2, tiploc, 7);
    struct TTTiploc *tpl = (struct TTTiploc *) hashmapGet(p->loc, tiploc);
    if (!tpl) {
        tpl = (struct TTTiploc *) malloc(sizeof (struct TTTiploc));

        if (tpl) {
            memset(tpl, 0, sizeof (struct TTTiploc));
            insert = 1;
        }
    }

    // Still no tpl then fail - out of memory most likely
    if (!tpl)
        return EXIT_FAILURE;

    int ofs = 2;

    ofs = cif_readString(parser->buf, ofs, tpl->tiploc, 7);

    // ignore caps?
    ofs += 2;

    ofs = cif_readInt_r(parser->buf, ofs, &tpl->nlc, 6);
    tpl->nlc_check = parser->buf[ofs++];

    ofs = cif_readString(parser->buf, ofs, tpl->desc, 26);

    ofs = cif_readInt_r(parser->buf, ofs, &tpl->stanox, 5);

    // PO MCP code ignored
    ofs += 4;

    ofs = cif_readString(parser->buf, ofs, tpl->crs, 3);
    
    ofs = cif_readString(parser->buf, ofs, tpl->nlcdesc, 16);

    if (insert || parser->buf[ofs] == ' ') {
        struct TTTiploc *old = (struct TTTiploc *) hashmapPut(p->loc, tpl->tiploc, tpl);
        if (old) {
            tpl->id = old->id;
            free(old);
        } else {
            tpl->id = p->locSeq++;
        }
    } else {
        char newt[8];
        cif_readString(parser->buf, ofs, newt, 7);
        logconsole("Tiploc change from '%s' to '%s'", tpl->tiploc, newt);

        // Remove old entry from the map
        struct TTTiploc *old = (struct TTTiploc *) hashmapRemove(p->loc, tpl->tiploc);
        if (old) {
            tpl->id = old->id;
            free(old);
        } else {
            tpl->id = p->locSeq++;
        }

        // Replace new tiploc & re-insert
        cif_readString(parser->buf, ofs, tpl->tiploc, 7);
        old = (struct TTTiploc *) hashmapPut(p->loc, tpl->tiploc, tpl);
        if (old)
            free(old);
    }

    return EXIT_SUCCESS;
}