Main Page | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

heap_bimodal.c File Reference


Detailed Description

bimodal heap management functions

This is the second of two implementations of heap management. It extends the simple malloc/free scheme by adding a single large allocation for requests smaller tha a certain size. This was added due to an apparent internal limit in malloc(3) or perhaps a kernel limit that after a certain number of calls and/or bytes of allocation and freeing, returns NULL for no apparent reason.

The common header file gc.h defines the prototypes for all heap allocation implementations by way of the CONFIG_HEAP_TYPE_xxx symbol definitions.

Here is a note taken from the original project README file:

(16) When nearing the end of the initial development, I ran across what is probably a memory configuration limit on my Solaris platform, which I did not bother to track down, but rather work around. It seems that when calling malloc(3C) or malloc(3MALLOC), after 2,280 malloc() allocations and 612 free() invocations, there is something under the covers that does a SIGSEGV, and it can happen in either routine. I therefore extended the heap mechanism to allocate 1M slots of 'n' bytes for small allocations up to this size. Everything else still uses malloc(). In this way, I was able to finish development on the JVM and release it to the ASF in a more timely manner. In other words, I will let the team fix it! I am not sure that the real project wants a static 'n + 1' MB data area just hanging around the runtime just because I did not take time to tune the system configuration!

This modified algorithm makes exactly two malloc() calls, one for an array of fixed size slots of (rbyte) and the other for an array of (rboolean) for rtrue/rfalse on whether a fixed-size memory slot is in use or not.

Control

$URL: https://svn.apache.org/path/name/heap_bimodal.c $ $Id: heap_bimodal.c 0 09/28/2005 dlydick $

Copyright 2005 The Apache Software Foundation or its licensors, as applicable.

Licensed under the Apache License, Version 2.0 ("the License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and limitations under the License.

Version:
$LastChangedRevision: 0 $
Date:
$LastChangedDate: 09/28/2005 $
Author:
$LastChangedBy: dlydick $ Original code contributed by Daniel Lydick on 09/28/2005.

Reference

Definition in file heap_bimodal.c.

#include "arch.h"
#include <errno.h>
#include <stdlib.h>
#include "jvmcfg.h"
#include "exit.h"
#include "gc.h"
#include "heap.h"
#include "jvmclass.h"
#include "util.h"

Go to the source code of this file.

Defines

#define HEAP_NUMBER_OF_SLOTS   1048576
 Number of slots of this size.
#define HEAP_SLOT_SIZE   160
 Small heap allocation area.
#define LOCATE_SLOT

Functions

static void heap_bimodal_c_dummy (void)
static rvoid heap_free_common_bimodal (rvoid *pheap_block)
 Release a previously allocated block back into the heap for future reallocation.
rvoid heap_free_data_bimodal (rvoid *pheap_block)
 Release a previously allocated data block back into the heap for future reallocation.
rvoid heap_free_method_bimodal (rvoid *pheap_block)
 Release a previously allocated method block back into the heap for future reallocation.
rvoid heap_free_stack_bimodal (rvoid *pheap_block)
 Release a previously allocated stack block back into the heap for future reallocation.
static rvoidheap_get_common_bimodal (int size, rboolean clrmem_flag)
 Allocate memory from heap to caller, judging which mode to use for allocation.
static rvoidheap_get_common_simple_bimodal (int size, rboolean clrmem_flag)
 Original heap allocation method that uses only malloc(3) and free(3).
rvoidheap_get_data_bimodal (int size, rboolean clrmem_flag)
 Allocate memory for a data area from heap to caller.
int heap_get_error_bimodal (rvoid *badptr)
 Allocation failure diagnostic.
rvoidheap_get_method_bimodal (int size, rboolean clrmem_flag)
 Allocate memory for a method from heap to caller.
rvoidheap_get_stack_bimodal (int size, rboolean clrmem_flag)
 Allocate memory for a stack area from heap to caller.
rvoid heap_init_bimodal ()
 Start up heap management methodology.
rvoid heap_shutdown_bimodal ()
 Shut down up heap management after JVM execution is finished.

Variables

static char * heap_bimodal_c_copyright = "\0" "$URL: https://svn.apache.org/path/name/heap_bimodal.c $ $Id: heap_bimodal.c 0 09/28/2005 dlydick $" " " "Copyright 2005 The Apache Software Foundation or its licensors, as applicable."
static rlong heap_free_count = 0
 Number of calls to free(3).
static int heap_last_errno = 0
 Most recent error code from malloc(3), for use by heap_get_error_bimodal(). Typically found in <errno.h> or <sys/errno.h>.
static rlong heap_malloc_count = 0
 Number of calls to malloc(3).
static rbytepheap_slot
 Pointer for physical allocation of heap block to manage.
static rbooleanpheap_slot_in_use
 Pointer for physical allocation for slots in use.
static rlong slot_alloc_count = 0
 Number of allocations made from pheap_slot.
static rlong slot_free_count = 0
 Number of allocations freed from pheap_slot.


Define Documentation

#define HEAP_SLOT_SIZE   160
 

Small heap allocation area.

For allocations up to n bytes, use this instead of system allocation and thus minimize number of calls to malloc() and free(). After 2,280 calls to malloc(3C) and 612 to free(3C), the library would kick out a SIGSEGV for no apparent reason. Use of malloc(3MALLOC) and free(3MALLOC) did the same thing, with -lmalloc. Use of -lbsdmalloc was at an even lower number. Therefore, it seems best to delay the issue (See quotation above from README file.)

The value of the HEAP_SLOT_SIZE definition ABSOLUTELY MUST BE A MULTIPLE of 4!!! (prefer 8 for future 64-bit implementation). This is sizeof(rvoid *) and must be such to always fundamentally avoid SIGSEGV on 4-byte accesses.

Definition at line 113 of file heap_bimodal.c.

Referenced by heap_free_common_bimodal(), and heap_init_bimodal().

#define HEAP_NUMBER_OF_SLOTS   1048576
 

Number of slots of this size.

Any number of slots is possible, up to the reasonable resource limits of the machine.

Definition at line 121 of file heap_bimodal.c.

Referenced by heap_init_bimodal().

#define LOCATE_SLOT
 


Function Documentation

static void heap_bimodal_c_dummy void   )  [static]
 

Definition at line 79 of file heap_bimodal.c.

rvoid heap_init_bimodal  ) 
 

Start up heap management methodology.

In a malloc/free scheme, there is nothing to do, but here, the two blocks and pheap_slot must be allocated and initialized.

Parameters: rvoid

Returns:
rvoid

Definition at line 149 of file heap_bimodal.c.

References EXIT_HEAP_ALLOC, exit_jvm(), HEAP_NUMBER_OF_SLOTS, HEAP_SLOT_SIZE, jvm_heap_initialized, pheap_slot, pheap_slot_in_use, rfalse, rnull, rtrue, and sysErrMsg().

static rvoid* heap_get_common_simple_bimodal int  size,
rboolean  clrmem_flag
[static]
 

Original heap allocation method that uses only malloc(3) and free(3).

Parameters:
size Number of bytes to allocate
clrmem_flag Set memory to all zeroes (rtrue) or not (rfalse). If rtrue, clear the allocated block, otherwise return it with its existing contents.
Returns:
(rvoid *) to allocated area. This pointer may be cast to any desired data type. If size of zero bytes is requested, return rnull and let caller croak on SIGSEGV. If no memory is available or some OS system call error happened, throw error, but do not return.
Exceptions:
JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR if no memory is available.
JVMCLASS_JAVA_LANG_INTERNALERROR if other allocation error.

Definition at line 287 of file heap_bimodal.c.

References EXIT_HEAP_ALLOC, exit_throw_exception(), GC_RUN, heap_last_errno, heap_malloc_count, JVMCLASS_JAVA_LANG_INTERNALERROR, JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR, rnull, and rtrue.

static rvoid* heap_get_common_bimodal int  size,
rboolean  clrmem_flag
[static]
 

Allocate memory from heap to caller, judging which mode to use for allocation.

When finished, this pointer should be sent back to heap_free_xxxx_bimodal() for reallocation.

Warning:
Much of the JVM initialization ABSOLUTELY DEPENDS on setting of the clrmem_flag value to rtrue so that allocated structures contain all zeroes. If the heap allocation scheme changes, this functionality needs to be brought forward or change much of the code, not only init code, but throughout the whole corpus.
Remarks:
If malloc(3) returns an error other than out of memory errors, then the system errno is saved out into heap_last_errno for retrieval by perror(3) or other user response. This is typically useful for system-level debugging when the OS or OS resources, security, etc., may be getting in the way of proper allocation.
Parameters:
size Number of bytes to allocate
clrmem_flag Set memory to all zeroes (rtrue) or not (rfalse). If rtrue, clear the allocated block, otherwise return it with its existing contents.
Returns:
(rvoid *) to allocated area. This pointer may be cast to any desired data type. If size of zero bytes is requested, return rnull and let caller croak on SIGSEGV. If no memory is available or some OS system call error happened, throw error, but do not return.
Exceptions:
JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR if no memory is available.
JVMCLASS_JAVA_LANG_INTERNALERROR if other allocation error.
< Typically found in <errno.h> or <sys/errno.h>

< Typically found in <errno.h> or <sys/errno.h>

Definition at line 420 of file heap_bimodal.c.

References rnull.

Referenced by heap_get_method_bimodal(), and heap_get_stack_bimodal().

rvoid* heap_get_method_bimodal int  size,
rboolean  clrmem_flag
 

Allocate memory for a method from heap to caller.

When finished, this pointer should be sent back to heap_free_method_bimodal() for reallocation.

Remarks:
This implementation makes no distinction betwen "method area heap" and any other usage. Other implementations may choose to implement the JVM Spec section 3.5.4 more rigorously.
Parameters:
size Number of bytes to allocate
clrmem_flag Set memory to all zeroes (rtrue) or not (rfalse)
Returns:
(rvoid *) to allocated area. This pointer may be cast to any desired data type. If size of zero bytes is requested, return rnull and let caller croak on SIGSEGV. If no memory is available or some OS system call error happened, throw error, but do not return.
Exceptions:
JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR if no memory is available.
JVMCLASS_JAVA_LANG_INTERNALERROR if other allocation error.

Definition at line 568 of file heap_bimodal.c.

References heap_get_common_bimodal().

rvoid* heap_get_stack_bimodal int  size,
rboolean  clrmem_flag
 

Allocate memory for a stack area from heap to caller.

When finished, this pointer should be sent back to heap_free_stack_bimodal() for reallocation.

Parameters:
size Number of bytes to allocate
clrmem_flag Set memory to all zeroes (rtrue) or not (rfalse)
Returns:
(rvoid *) to allocated area. This pointer may be cast to any desired data type. If size of zero bytes is requested, return rnull and let caller croak on SIGSEGV. If no memory is available or some OS system call error happened, throw error, but do not return.
Exceptions:
JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR if no memory is available.
JVMCLASS_JAVA_LANG_INTERNALERROR if other allocation error.

Definition at line 610 of file heap_bimodal.c.

References heap_get_common_bimodal().

rvoid* heap_get_data_bimodal int  size,
rboolean  clrmem_flag
 

Allocate memory for a data area from heap to caller.

When finished, this pointer should be sent back to heap_free_data_bimodal() for reallocation.

Parameters:
size Number of bytes to allocate
clrmem_flag Set memory to all zeroes (rtrue) or not (rfalse)
Returns:
(rvoid *) to allocated area. This pointer may be cast to any desired data type. If size of zero bytes is requested, return rnull and let caller croak on SIGSEGV. If no memory is available or some OS system call error happened, throw error, but do not return.
Exceptions:
JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR if no memory is available.
JVMCLASS_JAVA_LANG_INTERNALERROR if other allocation error.

Definition at line 652 of file heap_bimodal.c.

References rnull.

static rvoid heap_free_common_bimodal rvoid pheap_block  )  [static]
 

Release a previously allocated block back into the heap for future reallocation.

If a rnull pointer is passed in, ignore the request.

Parameters:
pheap_block An (rvoid *) previously returned by one of the heap_get_XXX_bimodal() functions.
Returns:
rvoid

Definition at line 678 of file heap_bimodal.c.

References HEAP_SLOT_SIZE, pheap_slot_in_use, rfalse, and slot_free_count.

Referenced by heap_free_method_bimodal(), and heap_free_stack_bimodal().

rvoid heap_free_method_bimodal rvoid pheap_block  ) 
 

Release a previously allocated method block back into the heap for future reallocation.

Remarks:
This implementation makes no distinction between method area heap and any other usage. Other implementations may choose to implement the JVM Spec section 3.5.4 more rigorously.
Parameters:
pheap_block An (rvoid *) previously returned by heap_get_method_bimodal()
Returns:
rvoid

Definition at line 741 of file heap_bimodal.c.

References heap_free_common_bimodal().

rvoid heap_free_stack_bimodal rvoid pheap_block  ) 
 

Release a previously allocated stack block back into the heap for future reallocation.

Parameters:
pheap_block An (rvoid *) previously returned by heap_get_stack_bimodal()
Returns:
rvoid

Definition at line 761 of file heap_bimodal.c.

References heap_free_common_bimodal().

rvoid heap_free_data_bimodal rvoid pheap_block  ) 
 

Release a previously allocated data block back into the heap for future reallocation.

Parameters:
pheap_block An (rvoid *) previously returned by heap_get_data_bimodal()
Returns:
rvoid

Definition at line 781 of file heap_bimodal.c.

References rnull.

int heap_get_error_bimodal rvoid badptr  ) 
 

Allocation failure diagnostic.

Returns an errno value per "errno.h" if a rnull pointer is passed in, namely from the most recent call to a heap allocation function. It may only be called once before the value is cleared. If a non-null pointer is passed in, ERROR0 is returned and the error status is again cleared.

Parameters:
badptr Return value from heap allocation function.
Returns:
ERROR0 when no error was found or non-null badptr given. heap_last_errno value otherwise.
< Typically found in <errno.h> or <sys/errno.h>

< Typically found in <errno.h> or <sys/errno.h>

< Typically found in <errno.h> or <sys/errno.h>

Definition at line 809 of file heap_bimodal.c.

rvoid heap_shutdown_bimodal  ) 
 

Shut down up heap management after JVM execution is finished.

Parameters: rvoid

Returns:
rvoid
< Typically found in <errno.h> or <sys/errno.h>

Definition at line 845 of file heap_bimodal.c.


Variable Documentation

char* heap_bimodal_c_copyright = "\0" "$URL: https://svn.apache.org/path/name/heap_bimodal.c $ $Id: heap_bimodal.c 0 09/28/2005 dlydick $" " " "Copyright 2005 The Apache Software Foundation or its licensors, as applicable." [static]
 

Definition at line 79 of file heap_bimodal.c.

rboolean* pheap_slot_in_use [static]
 

Pointer for physical allocation for slots in use.

Definition at line 127 of file heap_bimodal.c.

Referenced by heap_free_common_bimodal(), and heap_init_bimodal().

rbyte* pheap_slot [static]
 

Pointer for physical allocation of heap block to manage.

Definition at line 132 of file heap_bimodal.c.

Referenced by heap_init_bimodal().

int heap_last_errno = 0 [static]
 

Most recent error code from malloc(3), for use by heap_get_error_bimodal(). Typically found in <errno.h> or <sys/errno.h>.

Definition at line 200 of file heap_bimodal.c.

Referenced by heap_get_common_simple_bimodal(), and heap_get_error_simple().

rlong heap_malloc_count = 0 [static]
 

Number of calls to malloc(3).

One of four global variables providing rudimentary statistics for heap allocation history.

See also:
heap_free_count

malloc_free_count

slot_free_count

Definition at line 215 of file heap_bimodal.c.

Referenced by heap_get_common_simple_bimodal().

rlong heap_free_count = 0 [static]
 

Number of calls to free(3).

One of four global variables providing rudimentary statistics for heap allocation history.

See also:
heap_malloc_count

malloc_free_count

slot_free_count

Definition at line 227 of file heap_bimodal.c.

Referenced by heap_free_common_simple().

rlong slot_alloc_count = 0 [static]
 

Number of allocations made from pheap_slot.

One of four global variables providing rudimentary statistics for heap allocation history.

See also:
heap_malloc_count

heap_free_count

slot_free_count

Definition at line 239 of file heap_bimodal.c.

rlong slot_free_count = 0 [static]
 

Number of allocations freed from pheap_slot.

One of four global variables providing rudimentary statistics for heap allocation history.

See also:
heap_malloc_count

heap_free_count

slot_malloc_count

Definition at line 251 of file heap_bimodal.c.

Referenced by heap_free_common_bimodal().


Generated on Fri Sep 30 18:49:50 2005 by  doxygen 1.4.4