Пример #1
0
static char* test_remove2() {
	linked_list* list = llist_new();
	node* n = llist_insert(list, 1, 1);
	node* n2 = llist_insert(list, 2, 2);
	
	free(llist_remove(list, n));
	free(llist_remove(list, n2));
	
	mu_assert("** test_remove2: size != 0.", list->size == 0);
	mu_assert("** test_remove2: head != tail.", list->head == list->tail);
	mu_assert("** test_remove2: head != NULL.", list->head == NULL);
	
	return 0;
}
Пример #2
0
void hmap_set(struct hash_map *map, void *key, void *data)
{
	size_t hashed_key = map->hash_fn(key);
	struct linked_list *list = &map->buckets_array[hashed_key % map->num_buckets];
	struct ll_node *node;

	node = llist_search(list, _find_key, (void*)&hashed_key);
	if(!node) {
		if(!(node = llist_append(list))) {
			return;
		}
	}

	/* set data */
	{
		struct keydata_pair *kdp;

		if(!node->data) {
			if(!(node->data = (struct keydata_pair*)malloc(sizeof(struct keydata_pair)))) {
				llist_remove(list, node, NULL, NULL);
				ERROR("hmap_set: out of memory!");
				return;
			}
		}

		kdp = (struct keydata_pair*)node->data;
		kdp->hashed_key = hashed_key;
		kdp->data = data;
	}
}
Пример #3
0
void llist_free(llist *list) {
	element *pos = llist_first(list);
	while (!llist_isEmpty(list)) {
		pos = llist_remove(pos, list);
	}
	free(list->head);
	free(list);
}
Пример #4
0
/**
 * Put a job on foreground or background
 *
 * @author Renê de Souza Pinto
 * @param args List of arguments
 * @param where 1 - Foreground, 0 - Background
 * @return -1 on error, 0 otherwise
 */
int bf_g(char **args, int where) {

	linked_list *ltemp;
	job *jtemp;
	int num, st;
	char str_err[255];
	char founded = 0;
	siginfo_t sigi;
	unsigned long p;

	if(args != NULL)
		if(args[1] != NULL) {
			num = atoi(args[1]);
		} else {
			num = -1;
		}

	p     = 0;
	ltemp = jobs_list;
	while(ltemp != NULL) {
		jtemp = ltemp->element;

		if(jtemp != NULL) {
			if(jtemp->num == num) {
				founded = 1;
				break;
			} else {
				if(ltemp->next == NULL)
					break;
			}
		}

		ltemp = ltemp->next;
		p++;
	}

	if(num != -1 && !founded) {
		sprintf(str_err, "fg: %d: no such job\n", num);
		write(fd_out, str_err, strlen(str_err));
	} else if(num == -1 && !founded){
		if(where == 1) {
			sprintf(str_err, "fg: current: no such job\n");
		} else {
			sprintf(str_err, "bg: current: no such job\n");
		}
		write(fd_out, str_err, strlen(str_err));
	} else {
		llist_remove(&jobs_list, p);
		kill(jtemp->id, SIGCONT);

		if(where == 1) {
			//
		}
	}

}
Пример #5
0
/**
 * Remove adapter path from list
 */
static void remove_adapter(const char *path)
{
	adapter_object *a = get_adapter(path);

	if (a) {
		g_free(a->path);
		g_object_unref(a->proxy);
		g_free(a);
		llist_remove(adapters(), a);
	}
}
Пример #6
0
static void tcp_close(tcp_client *client)
{
	DEBUG("TCP: freeing client %p", client);

	shutdown(client->fd, SHUT_RDWR);
	close(client->fd);
	client->fd = -1;
	free(client->buf);
	client->buf = 0;
	llist_remove(tcp_clients(), client);
	free(client);
}
Пример #7
0
/**
 * Remove device path from device proxy list
 */
static void remove_device(const char *path)
{
	device_object *dev = get_device_object(path);

	if (dev) {
		g_free(dev->path);
		g_free(dev->addr);
		g_free(dev->adapter);
		g_object_unref(dev->proxy);
		g_free(dev);
		llist_remove(devices(), dev);
	}
}
Пример #8
0
int hash_map_remove(hash_map *hm, void *key) {
    int hash = hm->hash(key);
    assert(hash < hm->hashLength);

    llist_node *cur = hm->boxes[hash]->head;
    while(cur != NULL) {
        if(hm->comp(((hash_item*)cur->data)->key, key))
            break;
        cur = cur->next;
    }
    if(cur != NULL)
        return llist_remove(hm->boxes[hash], cur);
    return 0;
}
Пример #9
0
static char* test_remove3() {
	linked_list* list = llist_new();
	llist_insert(list, 1, 1);
	node* n = llist_insert(list, 2, 2);
	
	llist_remove(list, n);
	
	mu_assert("** test_remove3: head != tail.", list->head == list->tail);
	mu_assert("** test_remove3: head->next != NULL.", list->head->next == NULL);
	mu_assert("** test_remove3: head->prev != NULL.", list->head->prev == NULL);
	mu_assert("** test_remove3: removed node != n.", n->key == 2);
	
	free(n);
	
	return 0;
}
Пример #10
0
void *list_pop(struct list *list) {
  void *value;
  struct item *last;
  
  if (!list->last) {
    return NULL;
  }
  value = list->last->value;
  last = list->last;
  list->last = last->prev;
  if (!list->last) list->first = NULL;
  llist_remove(last);
  free(last);
  list->size--;
  
  return value;
}
Пример #11
0
/**
* Purpose:      Remove and free the memory of the list. Will remove all data
*               from the list if not empty.
* Parameters:   The list to be removed.
*/
void llist_removeList(llist *l) {
    llist_pos pos;

    list_entries = 0;

    if(l != NULL) {
        if(!llist_isempty(l)) {
            pos = llist_first(l);
            while(!llist_isempty(l)) {
                pos = llist_remove(pos, l);
            }
        }
        free(l->head);
        free(l);
    }

}
Пример #12
0
static char* test_remove4() {
	linked_list* list = llist_new();
	llist_insert(list, 1, 1);
	node* n = llist_insert(list, 2, 2);
	llist_insert(list, 3, 3);
	
	llist_remove(list, n);
	
	mu_assert("** test_remove4: head == tail.", list->head != list->tail);
	mu_assert("** test_remove4: head->next != tail.", list->head->next == list->tail);
	mu_assert("** test_remove4: tail->prev != head.", list->tail->prev == list->head);
	mu_assert("** test_remove4: head->prev != NULL.", list->head->prev == NULL);
	mu_assert("** test_remove4: tail->next != NULL.", list->tail->next == NULL);
	mu_assert("** test_remove4: head != 1.", list->head->key == 3);
	mu_assert("** test_remove4: tail != 3.", list->tail->key == 1);
	
	free(n);
	
	return 0; 
}
Пример #13
0
/**************************************************************************//**
*
* mss_timer_stop
*
* @brief      stop a running mss timer (timer state is reset back to 
*             MSS_TIMER_STATE_IDLE)
*
* @param[in]  hdl     timer handle
*
* @return     true if success, false if failed
*
******************************************************************************/
void mss_timer_stop(mss_timer_t hdl)
{
  mss_int_flag_t int_flag;

  // check timer handler
  MSS_DEBUG_CHECK(hdl != MSS_TIMER_INVALID_HDL);

  MSS_ENTER_CRITICAL_SECTION(int_flag);

  if(hdl->state & TIMER_ALL_RUNNING_MASK)
  {
    // search for the timer and remove it
    llist_remove(active_timer_llist, hdl);

    // set timer state as idle
    hdl->state = MSS_TIMER_STATE_IDLE;
  }

  MSS_LEAVE_CRITICAL_SECTION(int_flag);
}
Пример #14
0
static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
{
	llentry_t *e;

	if (list == NULL)
		return (-1);

	e = llist_search (list, name);
	if (e == NULL)
		return (-1);

	llist_remove (list, e);

	sfree (e->key);
	destroy_callback (e->value);

	llentry_destroy (e);

	return (0);
} /* }}} int plugin_unregister */
Пример #15
0
	//So I can compile/debug ;)
	int main()
	{
		int a = 42;
		int b = 1337;
		int c = 214;
		int d = 1234;
		int e = 5005;
		int f = 69;
		int place = 3;
		printf("---------------\n|** Excist ***|\n---------------\n");
		printf("%d\n", llist_excist());
		printf("---------------\n|*** Begin ***|\n---------------\n");
		llist_add(&a);
		llist_add(&b);
		llist_add(&c);
		llist_add(&d);
		llist_add(&e);
		printf("---------------\n|** Excist ***|\n---------------\n");
		printf("%d\n", llist_excist());
		//printf("Derde waarde: %d\n", *nodeList.firstNode->nextNode->nextNode->value);
		printf("---------------\n|*No of items*|\n---------------\n");
		printf("%d\n", llist_nrItems());
		printf("---------------\n|**Print func*|\n---------------\n");
		llist_show();
		printf("---------------\n|*Add on place|\n---------------\n");
		llist_add_on_place(&place,&f);
		printf("---------------\n|**Print func*|\n---------------\n");
		llist_show();
		printf("---------------\n|**Delet func*|\n---------------\n");
		llist_remove(42);
		llist_show();
		printf("---------------\n|** Excist ***|\n---------------\n");
		printf("%d\n", llist_excist());
		printf("---------------\n|**Clear func*|\n---------------\n");
		llist_clear();
		printf("---------------\n|** Excist ***|\n---------------\n");
		printf("%d\n", llist_excist());
		return 0;
	}
Пример #16
0
/**
 * Destroy health applications
 */
static void destroy_health_applications()
{
	DBusGProxy *proxy;

	proxy = dbus_g_proxy_new_for_name(conn, "org.bluez",
					  "/org/bluez", "org.bluez.HealthManager");

	if (!proxy) {
		ERROR("BlueZ health manager service not found");
		return;
	}
	
	while (apps()->first) {
		GError *error = NULL;
		struct app_object *app = apps()->first->element;

		DEBUG("Destroying %s", app->path);

		if (!dbus_g_proxy_call(proxy, "DestroyApplication",
				       &error,
				       DBUS_TYPE_G_OBJECT_PATH, app->path,
				       G_TYPE_INVALID,
				       G_TYPE_INVALID)) {
			if (error) {
				ERROR("Can't call DestroyApplication: %s", error->message);
				g_error_free(error);
			} else {
				DEBUG("Can't call DestroyApplication, probably disconnected");
			}
		}

		llist_remove(apps(), app);
		g_free(app->path);
		g_free(app);
	}
	llist_destroy(apps(), NULL);
	_apps = NULL;
	g_object_unref(proxy);
}
Пример #17
0
/**
 * Remove channel path from channel proxy list
 */
static void remove_channel(const char *path, int passive)
{
	channel_object *c = get_channel(path);

	if (c) {
		GError *error = NULL;
		device_object *d = get_device_object(c->device);

		if (d) {
			d->channels--;
		}

		if (d && !passive) {
			DEBUG("Destroying channel %s", path);
			if (!dbus_g_proxy_call(d->proxy, "DestroyChannel",
						&error,
						DBUS_TYPE_G_OBJECT_PATH,
						path,
						G_TYPE_INVALID,
						G_TYPE_INVALID)) {
				if (error) {
					DEBUG("Err destroying HDP channel %s",
								error->message);
					g_error_free(error);
				}
			}
		}

		g_free(c->path);
		g_free(c->device);
		g_object_unref(c->proxy);
		shutdown(c->fd, SHUT_RDWR);
		close(c->fd);
		c->fd = -1;
		llist_remove(channels(), c);
		g_free(c);
	}
}
Пример #18
0
/**
 * read_from_client
 *
 * Handles the activities that take place on the sockets monitored by
 * select. The activity includes new connections or data transfer from
 * existing connections.
 *
 * @param s Server struct, contains information about the server.
 */
void read_from_socket(server *s) {
	//int ret, maxi;
	client *c = NULL;
	node *n = NULL;

	//maxi = 0;

	/* Check if a client is trying to connect */
	pthread_mutex_trylock(&s->dataLock);
	if (FD_ISSET(s->listen_sd, &s->allset)) {

		/* get the client data ready */
		c = client_new();

		/* blocking call waiting for connections */
		c->fd = accept(s->listen_sd, (struct sockaddr *) &c->sa, &c->sa_len);
		/* make the client Socket non-blocking */
		if (fcntl(c->fd, F_SETFL, O_NONBLOCK | fcntl(c->fd, F_GETFL, 0)) == -1)
			SystemFatal("fcntl(): Client Non-Block Failed\n");

		fprintf(stdout, "Received connection from (%s, %d)\n",
			inet_ntoa(c->sa.sin_addr),
			ntohs(c->sa.sin_port));
		s->n_clients++;
		s->n_max_connected++;
		/*s->n_max_connected = (s->n_clients > s->n_max_connected) ?
				s->n_clients : s->n_max_connected;*/
		s->client_list = llist_append(s->client_list, (void *)c);
		fprintf(stdout, "Added client to list, new size: %d\n",
			llist_length(s->client_list));
		/* add the client to the list */
		/*for (i = 0; i < FD_SETSIZE; i++) {
			if (s->clientConn[i] == NULL) {
			s->clientConn[i] = c;
			//break;
			}
			if (s->clients[i] < 0) {
			s->clients[i] = c->fd;
			break;
			}
			if (i == FD_SETSIZE) {
			SystemFatal("To Many Clients\n");
			}
			}*/
		/* add the socket connections to the fd_set */
		/*FD_SET(c->fd, &s->allset);
		s->maxfd = (c->fd > s->maxfd) ?
		c->fd : s->maxfd;
		maxi = (i > maxi) ? i : maxi;*/
		c = NULL;
	}
	pthread_mutex_unlock(&s->dataLock);

	for (n = s->client_list->link; n != NULL; n = n->next) {

		/* check if the client cause an event */
		pthread_mutex_trylock(&s->dataLock);
		c = (client *)n->data;
		if (FD_ISSET(c->fd, &s->allset)) {
			process_client_req(c, s);
			//process_client_data(c, s);
		}
		pthread_mutex_unlock(&s->dataLock);

		//pthread_mutex_trylock(&s->dataLock);  TRY THIS LATER
		if (c->quit) {
			pthread_mutex_trylock(&s->dataLock);
			s->n_clients--;
			s->client_list = llist_remove(s->client_list, (void *)c, client_compare);
			fprintf(stderr, "[%5d]Removed client from list, new size: %d\n",
				c->fd, llist_length(s->client_list));
			close(c->fd);
			free(c);
			c = NULL;
			pthread_mutex_unlock(&s->dataLock);
		}
		//pthread_mutex_unlock(&s->dataLock);  TRY THIS LATER
	}

	/* go through the available connections */
	/*for(n = 0; n <= maxi; n++) {
		pthread_mutex_trylock(&s->dataLock);
		if(FD_ISSET(s->clients[n], &s->allset)){
		process_client_data();
		}
		pthread_mutex_unlock(&s->dataLock);
		}*/
	//free(c);
}