2012-11-03 15:49:15 +00:00
|
|
|
/* wmem_scopes.c
|
|
|
|
* Wireshark Memory Manager Scopes
|
|
|
|
* Copyright 2012, Evan Huus <eapache@gmail.com>
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-02-08 16:31:35 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2012-11-03 15:49:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2012-12-19 00:43:36 +00:00
|
|
|
#include "wmem_core.h"
|
2012-11-03 15:49:15 +00:00
|
|
|
#include "wmem_scopes.h"
|
2013-06-19 18:28:13 +00:00
|
|
|
#include "wmem_allocator.h"
|
2012-11-03 15:49:15 +00:00
|
|
|
|
2021-03-20 13:13:27 +00:00
|
|
|
#include <wsutil/ws_assert.h>
|
|
|
|
|
2012-11-03 15:49:15 +00:00
|
|
|
/* One of the supposed benefits of wmem over the old emem was going to be that
|
|
|
|
* the scoping of the various memory pools would be obvious, since they would
|
|
|
|
* no longer be global. Instead, the pools would be managed as variables scoped
|
|
|
|
* by the compiler, so functions outside of that scope wouldn't have access to
|
|
|
|
* the pools and wouldn't be able to allocate memory in a scope to which they
|
|
|
|
* didn't belong.
|
|
|
|
*
|
|
|
|
* That idea fell apart rather quickly :P
|
|
|
|
*
|
|
|
|
* The principle still stands, and most pools should be managed in that way.
|
|
|
|
* The three in this file are *exceptions*. They are the three scopes that emem
|
|
|
|
* provided as globals. Converting all of the code that used them to pass an
|
|
|
|
* extra parameter (or three) around would have been a nightmare of epic
|
|
|
|
* proportions, so we provide these three as globals still.
|
|
|
|
*
|
|
|
|
* We do, however, use some extra booleans and a mountain of assertions to try
|
|
|
|
* and catch anybody accessing the pools out of the correct scope. It's not
|
|
|
|
* perfect, but it should stop most of the bad behaviour that emem permitted.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* TODO: Make these thread-local */
|
|
|
|
static wmem_allocator_t *packet_scope = NULL;
|
|
|
|
static wmem_allocator_t *file_scope = NULL;
|
2012-11-24 18:47:50 +00:00
|
|
|
static wmem_allocator_t *epan_scope = NULL;
|
2012-11-03 15:49:15 +00:00
|
|
|
|
|
|
|
/* Packet Scope */
|
|
|
|
|
|
|
|
wmem_allocator_t *
|
|
|
|
wmem_packet_scope(void)
|
|
|
|
{
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(packet_scope);
|
2012-11-03 15:49:15 +00:00
|
|
|
|
2013-02-22 16:41:41 +00:00
|
|
|
return packet_scope;
|
2012-11-03 15:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_enter_packet_scope(void)
|
|
|
|
{
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(packet_scope);
|
|
|
|
ws_assert(file_scope->in_scope);
|
|
|
|
ws_assert(!packet_scope->in_scope);
|
2012-11-03 15:49:15 +00:00
|
|
|
|
2013-06-19 18:28:13 +00:00
|
|
|
packet_scope->in_scope = TRUE;
|
2012-11-03 15:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_leave_packet_scope(void)
|
|
|
|
{
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(packet_scope);
|
|
|
|
ws_assert(packet_scope->in_scope);
|
2012-11-03 15:49:15 +00:00
|
|
|
|
|
|
|
wmem_free_all(packet_scope);
|
2013-06-19 18:28:13 +00:00
|
|
|
packet_scope->in_scope = FALSE;
|
2012-11-03 15:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* File Scope */
|
|
|
|
|
|
|
|
wmem_allocator_t *
|
|
|
|
wmem_file_scope(void)
|
|
|
|
{
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(file_scope);
|
2012-11-03 15:49:15 +00:00
|
|
|
|
2013-02-22 16:41:41 +00:00
|
|
|
return file_scope;
|
2012-11-03 15:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_enter_file_scope(void)
|
|
|
|
{
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(file_scope);
|
|
|
|
ws_assert(!file_scope->in_scope);
|
2012-11-03 15:49:15 +00:00
|
|
|
|
2013-06-19 18:28:13 +00:00
|
|
|
file_scope->in_scope = TRUE;
|
2012-11-03 15:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_leave_file_scope(void)
|
|
|
|
{
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(file_scope);
|
|
|
|
ws_assert(file_scope->in_scope);
|
|
|
|
ws_assert(!packet_scope->in_scope);
|
2012-11-03 15:49:15 +00:00
|
|
|
|
|
|
|
wmem_free_all(file_scope);
|
2013-06-19 18:28:13 +00:00
|
|
|
file_scope->in_scope = FALSE;
|
2013-02-08 01:58:04 +00:00
|
|
|
|
|
|
|
/* this seems like a good time to do garbage collection */
|
|
|
|
wmem_gc(file_scope);
|
|
|
|
wmem_gc(packet_scope);
|
2012-11-03 15:49:15 +00:00
|
|
|
}
|
|
|
|
|
2012-11-24 18:47:50 +00:00
|
|
|
/* Epan Scope */
|
|
|
|
|
|
|
|
wmem_allocator_t *
|
|
|
|
wmem_epan_scope(void)
|
|
|
|
{
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(epan_scope);
|
2012-11-24 18:47:50 +00:00
|
|
|
|
|
|
|
return epan_scope;
|
|
|
|
}
|
|
|
|
|
2012-11-03 15:49:15 +00:00
|
|
|
/* Scope Management */
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_init_scopes(void)
|
|
|
|
{
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(packet_scope == NULL);
|
|
|
|
ws_assert(file_scope == NULL);
|
|
|
|
ws_assert(epan_scope == NULL);
|
2012-11-03 15:49:15 +00:00
|
|
|
|
2014-04-29 06:57:40 +00:00
|
|
|
packet_scope = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK_FAST);
|
2013-03-19 15:32:42 +00:00
|
|
|
file_scope = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK);
|
2015-07-04 01:14:53 +00:00
|
|
|
epan_scope = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK);
|
2013-06-19 18:28:13 +00:00
|
|
|
|
|
|
|
/* Scopes are initialized to TRUE by default on creation */
|
|
|
|
packet_scope->in_scope = FALSE;
|
|
|
|
file_scope->in_scope = FALSE;
|
2012-11-03 15:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_cleanup_scopes(void)
|
|
|
|
{
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(packet_scope);
|
|
|
|
ws_assert(file_scope);
|
|
|
|
ws_assert(epan_scope);
|
2012-11-03 15:49:15 +00:00
|
|
|
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(packet_scope->in_scope == FALSE);
|
|
|
|
ws_assert(file_scope->in_scope == FALSE);
|
2012-11-03 15:49:15 +00:00
|
|
|
|
|
|
|
wmem_destroy_allocator(packet_scope);
|
|
|
|
wmem_destroy_allocator(file_scope);
|
2012-11-24 18:47:50 +00:00
|
|
|
wmem_destroy_allocator(epan_scope);
|
2012-11-03 15:49:15 +00:00
|
|
|
|
|
|
|
packet_scope = NULL;
|
|
|
|
file_scope = NULL;
|
2012-11-24 18:47:50 +00:00
|
|
|
epan_scope = NULL;
|
2012-11-03 15:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2012-11-03 15:49:15 +00:00
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|