jdk/src/share/demo/jvmti/hprof/hprof_stack.c
author nloodin
Wed, 31 Aug 2011 13:48:04 +0200
changeset 10292 ed7db6a12c2a
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7067811: Update demo/sample code to state it should not be used for production Summary: Added comment block after copyright block stating that code is unfit for production. Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* Simple stack storage mechanism (or simple List). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Stack is any depth (grows as it needs to), elements are arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   length but known at stack init time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Stack elements can be accessed via pointers (be careful, if stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   moved while you point into stack you have problems)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Pointers to stack elements passed in are copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Since the stack can be inspected, it can be used for more than just
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *    a simple stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
resize(Stack *stack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    void  *old_elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    void  *new_elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int    old_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    int    new_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    HPROF_ASSERT(stack!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    HPROF_ASSERT(stack->elements!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    HPROF_ASSERT(stack->size>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    HPROF_ASSERT(stack->elem_size>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    HPROF_ASSERT(stack->incr_size>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    old_size     = stack->size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    old_elements = stack->elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    if ( (stack->resizes % 10) && stack->incr_size < (old_size >> 2) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        stack->incr_size = old_size >> 2; /* 1/4 the old_size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    new_size = old_size + stack->incr_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    new_elements = HPROF_MALLOC(new_size*stack->elem_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    (void)memcpy(new_elements, old_elements, old_size*stack->elem_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    stack->size     = new_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    stack->elements = new_elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    HPROF_FREE(old_elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    stack->resizes++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
Stack *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
stack_init(int init_size, int incr_size, int elem_size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    Stack *stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    void  *elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    HPROF_ASSERT(init_size>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    HPROF_ASSERT(elem_size>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    HPROF_ASSERT(incr_size>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    stack            = (Stack*)HPROF_MALLOC((int)sizeof(Stack));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    elements         = HPROF_MALLOC(init_size*elem_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    stack->size      = init_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    stack->incr_size = incr_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    stack->elem_size = elem_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    stack->count       = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    stack->elements  = elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    stack->resizes   = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    return stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
stack_element(Stack *stack, int i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    HPROF_ASSERT(stack!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    HPROF_ASSERT(stack->elements!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    HPROF_ASSERT(stack->count>i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    HPROF_ASSERT(i>=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    return (void*)(((char*)stack->elements) + i * stack->elem_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
stack_top(Stack *stack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    void *element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    HPROF_ASSERT(stack!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    element = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    if ( stack->count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        element = stack_element(stack, (stack->count-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    return element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
stack_depth(Stack *stack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    HPROF_ASSERT(stack!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    return stack->count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
stack_pop(Stack *stack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    void *element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    element = stack_top(stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    if ( element != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        stack->count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    return element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
stack_push(Stack *stack, void *element)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    void *top_element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    HPROF_ASSERT(stack!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    if ( stack->count >= stack->size ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        resize(stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    stack->count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    top_element = stack_top(stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    (void)memcpy(top_element, element, stack->elem_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
stack_term(Stack *stack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    HPROF_ASSERT(stack!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    if ( stack->elements != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        HPROF_FREE(stack->elements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    HPROF_FREE(stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}