jdk/src/jdk.runtime/share/native/common-unpack/bytes.h
author kevinw
Tue, 03 Mar 2015 19:42:09 +0000
changeset 29457 7cfe74042cef
parent 25859 3317bb8137f4
permissions -rw-r--r--
8073688: Infinite loop reading types during jmap attach. Reviewed-by: dsamersoff, sla
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: 1082
diff changeset
     2
 * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#ifdef WIN32_LEAN_AND_MEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
typedef signed char byte ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
struct bytes {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
  byte*  ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
  size_t len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
  byte*  limit() { return ptr+len; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  void set(byte* ptr_, size_t len_) { ptr = ptr_; len = len_; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  void set(const char* str)         { ptr = (byte*)str; len = strlen(str); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  bool inBounds(const void* p);     // p in [ptr, limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  void malloc(size_t len_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  void realloc(size_t len_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  void free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  void copyFrom(const void* ptr_, size_t len_, size_t offset = 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  void saveFrom(const void* ptr_, size_t len_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  void saveFrom(const char* str) { saveFrom(str, strlen(str)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  void copyFrom(bytes& other, size_t offset = 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    copyFrom(other.ptr, other.len, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  void saveFrom(bytes& other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    saveFrom(other.ptr, other.len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  void clear(int fill_byte = 0) { memset(ptr, fill_byte, len); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  byte* writeTo(byte* bp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  bool equals(bytes& other) { return 0 == compareTo(other); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  int compareTo(bytes& other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  bool contains(byte c) { return indexOf(c) >= 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  int indexOf(byte c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  // substrings:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  static bytes of(byte* ptr, size_t len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    bytes res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    res.set(ptr, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  bytes slice(size_t beg, size_t end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    bytes res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    res.ptr = ptr + beg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    res.len = end - beg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    assert(res.len == 0 || inBounds(res.ptr) && inBounds(res.limit()-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  // building C strings inside byte buffers:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  bytes& strcat(const char* str) { ::strcat((char*)ptr, str); return *this; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  bytes& strcat(bytes& other) { ::strncat((char*)ptr, (char*)other.ptr, other.len); return *this; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  char* strval() { assert(strlen((char*)ptr) == len); return (char*) ptr; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#ifdef PRODUCT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
  const char* string() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  const char* string();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
#define BYTES_OF(var) (bytes::of((byte*)&(var), sizeof(var)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
struct fillbytes {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
  bytes b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
  size_t allocated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
  byte*  base()               { return b.ptr; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
  size_t size()               { return b.len; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
  byte*  limit()              { return b.limit(); }          // logical limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
  void   setLimit(byte* lp)   { assert(isAllocated(lp)); b.len = lp - b.ptr; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
  byte*  end()                { return b.ptr + allocated; }  // physical limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
  byte*  loc(size_t o)        { assert(o < b.len); return b.ptr + o; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
  void   init()               { allocated = 0; b.set(null, 0); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  void   init(size_t s)       { init(); ensureSize(s); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
  void   free()               { if (allocated != 0) b.free(); allocated = 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  void   empty()              { b.len = 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
  byte*  grow(size_t s);      // grow so that limit() += s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
  int    getByte(uint i)      { return *loc(i) & 0xFF; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
  void   addByte(byte x)      { *grow(1) = x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
  void   ensureSize(size_t s); // make sure allocated >= s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
  void   trimToSize()         { if (allocated > size())  b.realloc(allocated = size()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  bool   canAppend(size_t s)  { return allocated > b.len+s; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
  bool   isAllocated(byte* p) { return p >= base() && p <= end(); } //asserts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
  void   set(bytes& src)      { set(src.ptr, src.len); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
  void set(byte* ptr, size_t len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    b.set(ptr, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    allocated = 0;   // mark as not reallocatable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
  // block operations on resizing byte buffer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
  fillbytes& append(const void* ptr_, size_t len_)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    { memcpy(grow(len_), ptr_, len_); return (*this); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  fillbytes& append(bytes& other)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    { return append(other.ptr, other.len); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
  fillbytes& append(const char* str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    { return append(str, strlen(str)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
struct ptrlist : fillbytes {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
  typedef const void* cvptr;
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   120
  int    length()     { return (int)(size() / sizeof(cvptr)); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
  cvptr* base()       { return (cvptr*) fillbytes::base(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
  cvptr& get(int i)   { return *(cvptr*)loc(i * sizeof(cvptr)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
  cvptr* limit()      { return (cvptr*) fillbytes::limit(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
  void   add(cvptr x) { *(cvptr*)grow(sizeof(x)) = x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
  void   popTo(int l) { assert(l <= length()); b.len = l * sizeof(cvptr); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
  int    indexOf(cvptr x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
  bool   contains(cvptr x) { return indexOf(x) >= 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
  void   freeAll();   // frees every ptr on the list, plus the list itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
// Use a macro rather than mess with subtle mismatches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
// between member and non-member function pointers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
#define PTRLIST_QSORT(ptrls, fn) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
  ::qsort((ptrls).base(), (ptrls).length(), sizeof(void*), fn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
struct intlist : fillbytes {
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   136
  int    length()     { return (int)(size() / sizeof(int)); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
  int*   base()       { return (int*) fillbytes::base(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
  int&   get(int i)   { return *(int*)loc(i * sizeof(int)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
  int*   limit()      { return (int*) fillbytes::limit(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
  void   add(int x)   { *(int*)grow(sizeof(x)) = x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
  void   popTo(int l) { assert(l <= length()); b.len = l * sizeof(int); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
  int    indexOf(int x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
  bool   contains(int x) { return indexOf(x) >= 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
};