wl_map_insert_new(struct wl_map *map, uint32_t flags, void *data)
{
	union map_entry *start, *entry;
	struct wl_array *entries;
	uint32_t base;
	uint32_t count;

	if (map->side == WL_MAP_CLIENT_SIDE) {
		entries = &map->client_entries;
		base = 0;
	} else {
		entries = &map->server_entries;
		base = WL_SERVER_ID_START;
	}

	if (map->free_list) {
		start = entries->data;
		entry = &start[map->free_list >> 1];
		map->free_list = entry->next;
	} else {
		entry = wl_array_add(entries, sizeof *entry);
		if (!entry)
			return 0;
		start = entries->data;
	}

	/* wl_array only grows, so if we have too many objects at
	 * this point there's no way to clean up. We could be more
	 * pro-active about trying to avoid this allocation, but
	 * it doesn't really matter because at this point there is
	 * nothing to be done but disconnect the client and delete
	 * the whole array either way.
	 */
	count = entry - start;
	if (count > WL_MAP_MAX_OBJECTS) {
		/* entry->data is freshly malloced garbage, so we'd
		 * better make it a NULL so wl_map_for_each doesn't
		 * dereference it later. */
		entry->data = NULL;
		return 0;
	}
	entry->data = data;
	entry->next |= (flags & 0x1) << 1;

	return count + base;
}
wl_map_insert_new(struct wl_map *map, uint32_t flags, void *data)
{
	union map_entry *start, *entry;
	struct wl_array *entries;
	uint32_t base;
	uint32_t count;

	if (map->side == WL_MAP_CLIENT_SIDE) {
		entries = &map->client_entries;
		base = 0;
	} else {
		entries = &map->server_entries;
		base = WL_SERVER_ID_START;
	}

	if (map->free_list) {
		start = entries->data;
		entry = &start[map->free_list >> 1];
		map->free_list = entry->next;
	} else {
		entry = wl_array_add(entries, sizeof *entry);
		if (!entry)
			return 0;
		start = entries->data;
	}

	 
	count = entry - start;
	if (count > WL_MAP_MAX_OBJECTS) {
		 
		entry->data = NULL;
		return 0;
	}
	entry->data = data;
	entry->next |= (flags & 0x1) << 1;

	return count + base;
}