Esempio n. 1
0
/**
 * Allocates an integer ID to be used for storing application-specific
 * data on any DBusServer. The allocated ID may then be used
 * with dbus_server_set_data() and dbus_server_get_data().
 * The slot must be initialized with -1. If a nonnegative
 * slot is passed in, the refcount is incremented on that
 * slot, rather than creating a new slot.
 *  
 * The allocated slot is global, i.e. all DBusServer objects will have
 * a slot with the given integer ID reserved.
 *
 * @param slot_p address of global variable storing the slot ID
 * @returns #FALSE on no memory
 */
dbus_bool_t
dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
{
  return _dbus_data_slot_allocator_alloc (&slot_allocator,
                                          (DBusMutex **)&_DBUS_LOCK_NAME (server_slots),
                                          slot_p);
}
Esempio n. 2
0
/**
 * Allocates an integer ID to be used for storing application-specific
 * data on any DBusPendingCall. The allocated ID may then be used
 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
 * The passed-in slot must be initialized to -1, and is filled in
 * with the slot ID. If the passed-in slot is not -1, it's assumed
 * to be already allocated, and its refcount is incremented.
 * 
 * The allocated slot is global, i.e. all DBusPendingCall objects will
 * have a slot with the given integer ID reserved.
 *
 * @param slot_p address of a global variable storing the slot
 * @returns #FALSE on failure (no memory)
 */
dbus_bool_t
dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
{
  _dbus_return_val_if_fail (slot_p != NULL, FALSE);

  return _dbus_data_slot_allocator_alloc (&slot_allocator,
                                          &_DBUS_LOCK_NAME (pending_call_slots),
                                          slot_p);
}
Esempio n. 3
0
/**
 * Allocates an integer ID to be used for storing application-specific
 * data on any DBusServer. The allocated ID may then be used
 * with dbus_server_set_data() and dbus_server_get_data().
 * The slot must be initialized with -1. If a nonnegative
 * slot is passed in, the refcount is incremented on that
 * slot, rather than creating a new slot.
 *
 * The allocated slot is global, i.e. all DBusServer objects will have
 * a slot with the given integer ID reserved.
 *
 * @param slot_p address of global variable storing the slot ID
 * @returns #FALSE on no memory
 */
dbus_bool_t
dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
{
    return _dbus_data_slot_allocator_alloc (&slot_allocator,
                                            slot_p);
}
dbus_bool_t
_dbus_data_slot_test (void)
{
    DBusDataSlotAllocator allocator;
    DBusDataSlotList list;
    int i;
    DBusFreeFunction old_free_func;
    void *old_data;
    DBusMutex *mutex;

    if (!_dbus_data_slot_allocator_init (&allocator))
        _dbus_assert_not_reached ("no memory for allocator");

    _dbus_data_slot_list_init (&list);

    _dbus_mutex_new_at_location (&mutex);
    if (mutex == NULL)
        _dbus_assert_not_reached ("failed to alloc mutex");

#define N_SLOTS 100

    i = 0;
    while (i < N_SLOTS)
    {
        /* we don't really want apps to rely on this ordered
         * allocation, but it simplifies things to rely on it
         * here.
         */
        dbus_int32_t tmp = -1;

        _dbus_data_slot_allocator_alloc (&allocator, &mutex, &tmp);

        if (tmp != i)
            _dbus_assert_not_reached ("did not allocate slots in numeric order\n");

        ++i;
    }

    i = 0;
    while (i < N_SLOTS)
    {
        if (!_dbus_data_slot_list_set (&allocator, &list,
                                       i,
                                       _DBUS_INT_TO_POINTER (i),
                                       test_free_slot_data_func,
                                       &old_free_func, &old_data))
            _dbus_assert_not_reached ("no memory to set data");

        _dbus_assert (old_free_func == NULL);
        _dbus_assert (old_data == NULL);

        _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
                      _DBUS_INT_TO_POINTER (i));

        ++i;
    }

    free_counter = 0;
    i = 0;
    while (i < N_SLOTS)
    {
        if (!_dbus_data_slot_list_set (&allocator, &list,
                                       i,
                                       _DBUS_INT_TO_POINTER (i),
                                       test_free_slot_data_func,
                                       &old_free_func, &old_data))
            _dbus_assert_not_reached ("no memory to set data");

        _dbus_assert (old_free_func == test_free_slot_data_func);
        _dbus_assert (_DBUS_POINTER_TO_INT (old_data) == i);

        (* old_free_func) (old_data);
        _dbus_assert (i == (free_counter - 1));

        _dbus_assert (_dbus_data_slot_list_get (&allocator, &list, i) ==
                      _DBUS_INT_TO_POINTER (i));

        ++i;
    }

    free_counter = 0;
    _dbus_data_slot_list_free (&list);

    _dbus_assert (N_SLOTS == free_counter);

    i = 0;
    while (i < N_SLOTS)
    {
        dbus_int32_t tmp = i;

        _dbus_data_slot_allocator_free (&allocator, &tmp);
        _dbus_assert (tmp == -1);
        ++i;
    }

    _dbus_mutex_free_at_location (&mutex);

    return TRUE;
}