Skip to content

Commit

Permalink
Automatically call grow when adding element to array and hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Nov 16, 2023
1 parent 43ea2db commit dd95ed4
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 86 deletions.
3 changes: 1 addition & 2 deletions src/internal_modules/roc_address/protocol_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,9 @@ bool ProtocolMap::get_supported_interfaces(core::Array<Interface>& interface_arr
}

if (n_iface == (unsigned)protos_[n_proto].iface) {
if (!interface_array.grow(interface_array.size() + 1)) {
if (!interface_array.push_back(protos_[n_proto].iface)) {
return false;
}
interface_array.push_back(protos_[n_proto].iface);
interfaces_exist = true;
break;
}
Expand Down
14 changes: 8 additions & 6 deletions src/internal_modules/roc_core/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,17 @@ template <class T, size_t EmbeddedCapacity = 0> class Array : public NonCopyable
}

//! Append element to array.
//! @pre
//! Array size() should be less than max_size().
void push_back(const T& value) {
if (size_ >= max_size_) {
roc_panic("array: attempting to append element to full array: size=%lu",
(unsigned long)size_);
//! @returns
//! false if the allocation failed
ROC_ATTR_NODISCARD bool push_back(const T& value) {
if (!grow_exp(size() + 1)) {
return false;
}

new (data_ + size_) T(value);
size_++;

return true;
}

//! Set array size.
Expand Down
22 changes: 14 additions & 8 deletions src/internal_modules/roc_core/hashmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class Hashmap : public NonCopyable<> {
const hashsum_t hash = T::key_hash(key);
HashmapNode::HashmapNodeData* node = impl_.find_node(
hash, (const void*)&key,
&Hashmap<T, EmbeddedCapacity, OwnershipPolicy>::key_equal<Key>);
&Hashmap<T, EmbeddedCapacity, OwnershipPolicy>::key_equal_<Key>);
if (!node) {
return NULL;
}
Expand Down Expand Up @@ -208,8 +208,10 @@ class Hashmap : public NonCopyable<> {
//! @remarks
//! - acquires ownership of @p element
//!
//! @returns
//! false if the allocation failed
//!
//! @pre
//! - hashmap size() should be smaller than hashmap capacity()
//! - @p element should not be member of any hashmap
//! - hashmap shouldn't have an element with the same key
//!
Expand All @@ -223,10 +225,13 @@ class Hashmap : public NonCopyable<> {
//! Insertion speed is higher when insert to remove ratio is close to one or lower,
//! and slows down when it becomes higher than one. The slow down is caused by
//! the incremental rehashing algorithm.
void insert(T& element) {
ROC_ATTR_NODISCARD bool insert(T& element) {
HashmapNode::HashmapNodeData* node = element.hashmap_node_data();
insert_(element.key(), node);
if (!insert_(element.key(), node)) {
return false;
}
OwnershipPolicy<T>::acquire(element);
return true;
}

//! Remove element from hashmap.
Expand Down Expand Up @@ -284,17 +289,18 @@ class Hashmap : public NonCopyable<> {
}

template <class Key>
static bool key_equal(HashmapNode::HashmapNodeData* node, const void* key) {
static bool key_equal_(HashmapNode::HashmapNodeData* node, const void* key) {
T* elem = container_of_(node);
const Key& key_ref = *(const Key*)key;
return T::key_equal(elem->key(), key_ref);
}

template <class Key>
void insert_(const Key& key, HashmapNode::HashmapNodeData* node) {
bool insert_(const Key& key, HashmapNode::HashmapNodeData* node) {
const hashsum_t hash = T::key_hash(key);
impl_.insert(node, hash, (const void*)&key,
&Hashmap<T, EmbeddedCapacity, OwnershipPolicy>::key_equal<Key>);
return impl_.insert(
node, hash, (const void*)&key,
&Hashmap<T, EmbeddedCapacity, OwnershipPolicy>::key_equal_<Key>);
}

AlignedStorage<NumEmbeddedBuckets * sizeof(HashmapImpl::Bucket)> embedded_buckets_;
Expand Down
8 changes: 6 additions & 2 deletions src/internal_modules/roc_core/hashmap_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ HashmapImpl::nextof(HashmapNode::HashmapNodeData* node) const {
return node->all_next;
}

void HashmapImpl::insert(HashmapNode::HashmapNodeData* node,
bool HashmapImpl::insert(HashmapNode::HashmapNodeData* node,
hashsum_t hash,
const void* key,
key_equals_callback key_equals) {
if (size_ >= buckets_capacity_(n_curr_buckets_)) {
roc_panic("hashmap: attempt to insert into full hashmap before calling grow()");
if (!grow()) {
return false;
}
}

if (node->bucket != NULL) {
Expand All @@ -131,6 +133,8 @@ void HashmapImpl::insert(HashmapNode::HashmapNodeData* node,
size_++;

proceed_rehash_(true);

return true;
}

void HashmapImpl::remove(HashmapNode::HashmapNodeData* node, bool skip_rehash) {
Expand Down
2 changes: 1 addition & 1 deletion src/internal_modules/roc_core/hashmap_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class HashmapImpl {
HashmapNode::HashmapNodeData* nextof(HashmapNode::HashmapNodeData* node) const;

//! Insert node into hashmap.
void insert(HashmapNode::HashmapNodeData* node,
bool insert(HashmapNode::HashmapNodeData* node,
hashsum_t hash,
const void* key,
key_equals_callback callback);
Expand Down
3 changes: 1 addition & 2 deletions src/internal_modules/roc_node/receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,12 @@ core::SharedPtr<Receiver::Slot> Receiver::get_slot_(slot_index_t slot_index,
return NULL;
}

if (!slot_map_.grow()) {
if (!slot_map_.insert(*slot)) {
roc_log(LogError, "receiver node: failed to create slot %lu",
(unsigned long)slot_index);
return NULL;
}

slot_map_.insert(*slot);
} else {
roc_log(LogError, "receiver node: failed to find slot %lu",
(unsigned long)slot_index);
Expand Down
4 changes: 1 addition & 3 deletions src/internal_modules/roc_node/sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,11 @@ core::SharedPtr<Sender::Slot> Sender::get_slot_(slot_index_t slot_index,
return NULL;
}

if (!slot_map_.grow()) {
if (!slot_map_.insert(*slot)) {
roc_log(LogError, "sender node: failed to create slot %lu",
(unsigned long)slot_index);
return NULL;
}

slot_map_.insert(*slot);
} else {
roc_log(LogError, "sender node: failed to find slot %lu",
(unsigned long)slot_index);
Expand Down
11 changes: 5 additions & 6 deletions src/internal_modules/roc_packet/router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ Router::Router(core::IArena& arena)
}

bool Router::add_route(IWriter& writer, unsigned flags) {
if (!routes_.grow_exp(routes_.size() + 1)) {
roc_log(LogError, "router: can't allocate route");
return false;
}

Route r;
r.writer = &writer;
r.flags = flags;
r.source = 0;
r.has_source = false;

routes_.push_back(r);
if (!routes_.push_back(r)) {
roc_log(LogError, "router: can't allocate route");
return false;
}

return true;
}

Expand Down
12 changes: 5 additions & 7 deletions src/internal_modules/roc_rtp/format_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ bool FormatMap::add_format(const Format& fmt) {
roc_panic("format map: bad format: invalid codec functions");
}

if (!node_map_.grow()) {
roc_log(LogError,
"format map: failed to register format: hashmap allocation failed");
return false;
}

if (node_map_.find(fmt.payload_type)) {
roc_log(LogError,
"format map: failed to register format: payload type %u already exists",
Expand All @@ -106,7 +100,11 @@ bool FormatMap::add_format(const Format& fmt) {
return false;
}

node_map_.insert(*node);
if (!node_map_.insert(*node)) {
roc_log(LogError,
"format map: failed to register format: hashmap allocation failed");
return false;
}

return true;
}
Expand Down
6 changes: 2 additions & 4 deletions src/internal_modules/roc_sdp/media_description.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,10 @@ bool MediaDescription::set_nb_ports(long nb_ports) {
}

bool MediaDescription::add_payload_id(unsigned payload_id) {
if (!payload_ids_.grow_exp(payload_ids_.size() + 1)) {
if (!payload_ids_.push_back(payload_id)) {
return false;
}

payload_ids_.push_back(payload_id);
return true;
}

Expand All @@ -114,11 +113,10 @@ bool MediaDescription::add_connection_data(address::AddrFamily addrtype,
return false;
}

if (!connection_data_.grow_exp(connection_data_.size() + 1)) {
if (!connection_data_.push_back(c)) {
return false;
}

connection_data_.push_back(c);
return true;
}

Expand Down
14 changes: 8 additions & 6 deletions src/internal_modules/roc_sndio/backend_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,13 @@ void BackendMap::set_frame_size(core::nanoseconds_t frame_length,
}

void BackendMap::register_backends_() {
if (!backends_.grow(MaxBackends)) {
roc_panic("backend map: can't grow backends array");
}

#ifdef ROC_TARGET_PULSEAUDIO
pulseaudio_backend_.reset(new (pulseaudio_backend_) PulseaudioBackend);
backends_.push_back(pulseaudio_backend_.get());
add_backend_(pulseaudio_backend_.get());
#endif // ROC_TARGET_PULSEAUDIO
#ifdef ROC_TARGET_SOX
sox_backend_.reset(new (sox_backend_) SoxBackend);
backends_.push_back(sox_backend_.get());
add_backend_(sox_backend_.get());
#endif // ROC_TARGET_SOX
}

Expand All @@ -66,5 +62,11 @@ void BackendMap::register_drivers_() {
}
}

void BackendMap::add_backend_(IBackend* backend) {
if (!backends_.push_back(backend)) {
roc_panic("backend map: can't register backend");
}
}

} // namespace sndio
} // namespace roc
2 changes: 2 additions & 0 deletions src/internal_modules/roc_sndio/backend_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class BackendMap : public core::NonCopyable<> {
void register_backends_();
void register_drivers_();

void add_backend_(IBackend*);

#ifdef ROC_TARGET_PULSEAUDIO
core::Optional<PulseaudioBackend> pulseaudio_backend_;
#endif // ROC_TARGET_PULSEAUDIO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ PulseaudioBackend::PulseaudioBackend() {

void PulseaudioBackend::discover_drivers(
core::Array<DriverInfo, MaxDrivers>& driver_list) {
if (!driver_list.grow(driver_list.size() + 1)) {
roc_panic("pulseaudio backend: can't grow drivers array");
if (!driver_list.push_back(DriverInfo("pulse", DriverType_Device,
DriverFlag_IsDefault | DriverFlag_SupportsSink
| DriverFlag_SupportsSource,
this))) {
roc_panic("pulseaudio backend: can't add driver");
}

driver_list.push_back(DriverInfo("pulse", DriverType_Device,
DriverFlag_IsDefault | DriverFlag_SupportsSink
| DriverFlag_SupportsSource,
this));
}

IDevice* PulseaudioBackend::open_device(DeviceType device_type,
Expand Down
26 changes: 12 additions & 14 deletions src/internal_modules/roc_sndio/target_sox/roc_sndio/sox_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,13 @@ void SoxBackend::discover_drivers(core::Array<DriverInfo, MaxDrivers>& driver_li

const char* driver = map_from_sox_driver(default_drivers[n]);

if (!driver_list.grow(driver_list.size() + 1)) {
roc_panic("sox backend: can't grow drivers array");
if (!driver_list.push_back(DriverInfo(driver, DriverType_Device,
DriverFlag_IsDefault
| DriverFlag_SupportsSource
| DriverFlag_SupportsSink,
this))) {
roc_panic("sox backend: can't add driver");
}

driver_list.push_back(DriverInfo(driver, DriverType_Device,
DriverFlag_IsDefault | DriverFlag_SupportsSource
| DriverFlag_SupportsSink,
this));
}

const sox_format_tab_t* formats = sox_get_format_fns();
Expand All @@ -215,14 +214,13 @@ void SoxBackend::discover_drivers(core::Array<DriverInfo, MaxDrivers>& driver_li
continue;
}

if (!driver_list.grow(driver_list.size() + 1)) {
roc_panic("sox backend: can't grow drivers array");
if (!driver_list.push_back(DriverInfo(
driver,
(handler->flags & SOX_FILE_DEVICE) ? DriverType_Device
: DriverType_File,
DriverFlag_SupportsSource | DriverFlag_SupportsSink, this))) {
roc_panic("sox backend: can't add driver");
}

driver_list.push_back(DriverInfo(
driver,
(handler->flags & SOX_FILE_DEVICE) ? DriverType_Device : DriverType_File,
DriverFlag_SupportsSource | DriverFlag_SupportsSink, this));
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/tests/roc_core/test_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ TEST(array, push_back) {
CHECK(array.grow(NumObjects));

for (size_t n = 0; n < NumObjects; n++) {
array.push_back(Object(n));
CHECK(array.push_back(Object(n)));

LONGS_EQUAL(NumObjects, array.capacity());
LONGS_EQUAL(n + 1, array.size());
Expand Down Expand Up @@ -174,9 +174,9 @@ TEST(array, constructor_destructor) {

CHECK(array.grow(3));

array.push_back(Object(1));
array.push_back(Object(2));
array.push_back(Object(3));
CHECK(array.push_back(Object(1)));
CHECK(array.push_back(Object(2)));
CHECK(array.push_back(Object(3)));

LONGS_EQUAL(0, arena.num_allocations());
LONGS_EQUAL(3, Object::n_objects);
Expand All @@ -186,8 +186,8 @@ TEST(array, constructor_destructor) {
LONGS_EQUAL(1, arena.num_allocations());
LONGS_EQUAL(3, Object::n_objects);

array.push_back(Object(4));
array.push_back(Object(5));
CHECK(array.push_back(Object(4)));
CHECK(array.push_back(Object(5)));

LONGS_EQUAL(1, arena.num_allocations());
LONGS_EQUAL(5, Object::n_objects);
Expand Down
Loading

0 comments on commit dd95ed4

Please sign in to comment.