Skip to content

Add umf_ba_linear_pool_contains_pointer #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

ldorau
Copy link
Contributor

@ldorau ldorau commented Feb 7, 2024

Add umf_ba_linear_pool_contains_pointer().
It returns:

  • 0 if ptr does not belong to the pool or
  • size (> 0) of the memory region from ptr to the end of the pool if ptr belongs to the pool.

It will be useful to implement realloc() in the proxy library.

Requires:

Make it possible to allocate more than the initial pool size
of the linear allocator.

Signed-off-by: Lukasz Dorau <[email protected]>
@ldorau
Copy link
Contributor Author

ldorau commented Feb 7, 2024

BTW: the code was copied from #218 by @igchor

@igchor
Copy link
Member

igchor commented Feb 7, 2024

Looks good. But can you explain why this will be needed for proxy pool? I still don't understand why we can't just detect recursion in malloc/free (globally, not per pointer) and then call appropriate function (base_alloc or umf*)

@ldorau
Copy link
Contributor Author

ldorau commented Feb 7, 2024

Looks good. But can you explain why this will be needed for proxy pool? I still don't understand why we can't just detect recursion in malloc/free (globally, not per pointer) and then call appropriate function (base_alloc or umf*)

If free() is called from "outside world" (not from inside of malloc()), it does not know if the pointer it gets comes from the base allocator or from the pool manager, so it has to check that.

@igchor
Copy link
Member

igchor commented Feb 7, 2024

Looks good. But can you explain why this will be needed for proxy pool? I still don't understand why we can't just detect recursion in malloc/free (globally, not per pointer) and then call appropriate function (base_alloc or umf*)

If free() is called from "outside world" (not from inside of malloc()), it does not know if the pointer it gets comes from the base allocator or from the pool manager, so it has to check that.

How can a pointer from the 'outside world' come from the base allocator? If a user calls 'free', the pointer has to come from a 'malloc' which means it has to be part of the umf pool, right?

@bratpiorka
Copy link
Contributor

this code looks ok but please don't merge unless we need it

Add umf_ba_linear_pool_contains_pointer().
It returns:
- 0 if ptr does not belong to the pool or
- size (> 0) of the memory region from ptr
  to the end of the pool if ptr belongs to the pool.

It will be useful to implement realloc() in the proxy library.

Signed-off-by: Lukasz Dorau <[email protected]>
@ldorau ldorau force-pushed the Add_umf_ba_linear_pool_contains_pointer branch from f764b67 to 3e27c1e Compare February 8, 2024 09:24
@ldorau
Copy link
Contributor Author

ldorau commented Feb 8, 2024

Looks good. But can you explain why this will be needed for proxy pool? I still don't understand why we can't just detect recursion in malloc/free (globally, not per pointer) and then call appropriate function (base_alloc or umf*)

If free() is called from "outside world" (not from inside of malloc()), it does not know if the pointer it gets comes from the base allocator or from the pool manager, so it has to check that.

How can a pointer from the 'outside world' come from the base allocator? If a user calls 'free', the pointer has to come from a 'malloc' which means it has to be part of the umf pool, right?

@igchor Hundreds of pointers can come from the base allocator:

  1. An app starts with LD_PRELOAD=proxy_lib
  2. libc calls malloc() ... (they come from the base allocator):
>>> malloc(72704)
>>> realloc(0x0, 416)
>>> malloc(20)
>>> malloc(24)
  1. The constructor of proxy_lib starts ...
  2. The constructor of proxy_lib calls umfMemoryProviderCreate(umfOsMemoryProviderOps()) and hwloc calls hundreds of malloc()s (they come from the base allocator):
>>> malloc(896)
>>> malloc(32)
>>> malloc(32816)
>>> malloc(100)
>>> malloc(44)
...
  1. The constructor of proxy_lib calls umfPoolCreate(umfJemallocPoolOps())
  2. The UMF pool is created. All allocations starting from this point come from the UMF pool.
    ...
  3. An app calls free(ptr) - does the ptr come from the base allocator or from the UMF pool ???

@ldorau
Copy link
Contributor Author

ldorau commented Feb 8, 2024

Closing for now - it will be reopened as a part of the pull request with the proxy library ...

@ldorau ldorau closed this Feb 8, 2024
@ldorau ldorau deleted the Add_umf_ba_linear_pool_contains_pointer branch February 22, 2024 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants