Skip to content

Commit

Permalink
Merge pull request #2077 from DOCGroup/jwi-shmem
Browse files Browse the repository at this point in the history
Fix ACE_Shared_Memory_Pool
  • Loading branch information
jwillemsen authored Jun 21, 2023
2 parents 32ea782 + 3b4af68 commit 7ee0839
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 152 deletions.
2 changes: 2 additions & 0 deletions ACE/NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
USER VISIBLE CHANGES BETWEEN ACE-7.1.0 and ACE-7.1.1
====================================================

. Fixed shared memory leak by ACE_Shared_Memory_Pool

USER VISIBLE CHANGES BETWEEN ACE-7.0.11 and ACE-7.1.0
=====================================================

Expand Down
1 change: 0 additions & 1 deletion ACE/ace/MMAP_Memory_Pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ ACE_MMAP_Memory_Pool::map_file (size_t map_size)
// Ask operating system for more shared memory, increasing the mapping
// accordingly. Note that this routine assumes that the appropriate
// locks are held when it is called.

void *
ACE_MMAP_Memory_Pool::acquire (size_t nbytes,
size_t &rounded_bytes)
Expand Down
24 changes: 10 additions & 14 deletions ACE/ace/Malloc_T.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "ace/ACE.h"
#include "ace/OS_NS_string.h"
#include <cstring>

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

Expand Down Expand Up @@ -427,7 +428,6 @@ ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::free (void *ptr)
// rounding...). Depending on the type of <MEM_POOL> (i.e., shared
// vs. local) subsequent calls from other processes will only
// initialize the control block pointer.

template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB> int
ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::open ()
{
Expand Down Expand Up @@ -577,7 +577,6 @@ ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::~ACE_Malloc_T ()
}

// Clean up the resources allocated by ACE_Malloc_T.

template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB> int
ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::remove ()
{
Expand All @@ -601,7 +600,7 @@ ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::remove ()
// Also notice that we are leaving the decision of removing
// the pool to users so they can map to the same mmap file
// again.
this->cb_ptr_ = 0;
this->cb_ptr_ = nullptr;

return result;
}
Expand Down Expand Up @@ -833,16 +832,15 @@ ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::shared_find (const char *name)
ACE_TRACE ("ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::shared_find");
#endif /* !ACE_HAS_WIN32_STRUCTURED_EXCEPTIONS */

if (this->cb_ptr_ == 0)
return 0;
if (!this->cb_ptr_)
return nullptr;

ACE_SEH_TRY
{
for (NAME_NODE *node = this->cb_ptr_->name_head_;
node != 0;
node = node->next_)
if (ACE_OS::strcmp (node->name (),
name) == 0)
if (std::strcmp (node->name (), name) == 0)
return node;
}
ACE_SEH_EXCEPT (this->memory_pool_.seh_selector (GetExceptionInformation ()))
Expand All @@ -855,11 +853,11 @@ template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB> int
ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::shared_bind (const char *name,
void *pointer)
{
if (this->cb_ptr_ == 0)
if (!this->cb_ptr_)
return -1;

// Combine the two allocations into one to avoid overhead...
NAME_NODE *new_node = 0;
NAME_NODE *new_node = nullptr;

ACE_ALLOCATOR_RETURN (new_node,
(NAME_NODE *)
Expand Down Expand Up @@ -991,7 +989,7 @@ ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::unbind (const char *name, void *
curr != 0;
curr = curr->next_)
{
if (ACE_OS::strcmp (curr->name (), name) == 0)
if (std::strcmp (curr->name (), name) == 0)
{
pointer = (char *) curr->pointer_;

Expand Down Expand Up @@ -1127,8 +1125,7 @@ ACE_Malloc_LIFO_Iterator_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::advance ()
return this->curr_ != 0;

while (this->curr_ != 0
&& ACE_OS::strcmp (this->name_,
this->curr_->name ()) != 0)
&& std::strcmp (this->name_, this->curr_->name ()) != 0)
this->curr_ = this->curr_->next_;

return this->curr_ != 0;
Expand Down Expand Up @@ -1223,8 +1220,7 @@ ACE_Malloc_FIFO_Iterator_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::advance ()
return this->curr_ != 0;

while (this->curr_ != 0
&& ACE_OS::strcmp (this->name_,
this->curr_->name ()) != 0)
&& std::strcmp (this->name_, this->curr_->name ()) != 0)
this->curr_ = this->curr_->prev_;

return this->curr_ != 0;
Expand Down
3 changes: 3 additions & 0 deletions ACE/ace/Malloc_T.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ class ACE_Malloc_T
int ref_counter ();

/// Release ref counter.
/// @retval 0 Success
/// @retval -1 Failure due to missing control block
/// @retval >0 Memory not release because refcount is not zero
int release (int close = 0);

/// Releases resources allocated by this object.
Expand Down
10 changes: 5 additions & 5 deletions ACE/ace/Malloc_T.inl
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB> ACE_INLINE int
ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::release (int close)
{
ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *this->lock_, -1);
if (this->cb_ptr_ != 0)
if (this->cb_ptr_ != nullptr)
{
int const retv = --this->cb_ptr_->ref_counter_;

if (close)
this->memory_pool_.release (0);
{
this->memory_pool_.release (0);
}

if (retv == 0)
{
Expand Down Expand Up @@ -109,9 +111,7 @@ ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::protect (ssize_t len,
}

template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB> ACE_INLINE int
ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::protect (void *addr,
size_t len,
int flags)
ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>::protect (void *addr, size_t len, int flags)
{
ACE_TRACE ("ACE_Malloc_T<MEMORY_POOL, ACE_LOCK, ACE_CB>::protect");
return this->memory_pool_.protect (addr, len, flags);
Expand Down
2 changes: 0 additions & 2 deletions ACE/ace/Mem_Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ ACE_Mem_Map::ACE_Mem_Map ()
}

// Map a file specified by FILE_NAME.

ACE_Mem_Map::ACE_Mem_Map (const ACE_TCHAR *file_name,
size_t len,
int flags,
Expand Down Expand Up @@ -279,7 +278,6 @@ ACE_Mem_Map::ACE_Mem_Map (const ACE_TCHAR *file_name,

// Map a file from an open file descriptor HANDLE. This function will
// lookup the length of the file if it is not given.

ACE_Mem_Map::ACE_Mem_Map (ACE_HANDLE handle,
size_t len,
int prot,
Expand Down
30 changes: 15 additions & 15 deletions ACE/ace/SV_Shared_Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,29 @@ class ACE_Export ACE_SV_Shared_Memory
ACE_SV_Shared_Memory (ACE_HANDLE internal_id,
int flags = 0);

int open (key_t external_id,
size_t size,
int create = ACE_SV_Shared_Memory::ACE_OPEN,
int perms = ACE_DEFAULT_FILE_PERMS);

int open_and_attach (key_t external_id,
size_t size,
int create = ACE_SV_Shared_Memory::ACE_OPEN,
int perms = ACE_DEFAULT_FILE_PERMS,
void *virtual_addr = 0,
int flags = 0);
int open (key_t external_id,
size_t size,
int create = ACE_SV_Shared_Memory::ACE_OPEN,
int perms = ACE_DEFAULT_FILE_PERMS);

int open_and_attach (key_t external_id,
size_t size,
int create = ACE_SV_Shared_Memory::ACE_OPEN,
int perms = ACE_DEFAULT_FILE_PERMS,
void *virtual_addr = 0,
int flags = 0);

/// Attach this shared memory segment.
int attach (void *virtual_addr = 0, int flags = 0);
int attach (void *virtual_addr = 0, int flags = 0);

/// Detach this shared memory segment.
int detach ();
int detach ();

/// Remove this shared memory segment.
int remove ();
int remove ();

/// Forward to underlying System V <shmctl>.
int control (int cmd, void *buf);
int control (int cmd, void *buf);

// = Segment-related info.
void *get_segment_ptr () const;
Expand Down
2 changes: 1 addition & 1 deletion ACE/ace/Shared_Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ACE_BEGIN_VERSIONED_NAMESPACE_DECL
* This is a very simple-minded wrapper, i.e., it really is only
* useful for allocating large contiguous chunks of shared
* memory. For a much more sophisticated version, please check
* out the <ACE_Malloc> class.
* out the ACE_Malloc class.
*/
class ACE_Export ACE_Shared_Memory
{
Expand Down
2 changes: 1 addition & 1 deletion ACE/ace/Shared_Memory_MM.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ACE_Export ACE_Shared_Memory_MM : public ACE_Shared_Memory
virtual void *malloc (size_t size = 0);

/// Free a chuck of memory allocated by
/// <ACE_Shared_Memory_MM::malloc>.
/// ACE_Shared_Memory_MM::malloc.
virtual int free (void *p);

/// Return the size of the shared memory segment.
Expand Down
Loading

0 comments on commit 7ee0839

Please sign in to comment.