summaryrefslogtreecommitdiff
path: root/storage/ibmdb2i/db2i_blobCollection.h
blob: 6a60394555fb387324461a54e5e5223a687b10d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Licensed Materials - Property of IBM
DB2 Storage Engine Enablement
Copyright IBM Corporation 2007,2008
All rights reserved

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met: 
 (a) Redistributions of source code must retain this list of conditions, the
     copyright notice in section {d} below, and the disclaimer following this
     list of conditions. 
 (b) Redistributions in binary form must reproduce this list of conditions, the
     copyright notice in section (d) below, and the disclaimer following this
     list of conditions, in the documentation and/or other materials provided
     with the distribution. 
 (c) The name of IBM may not be used to endorse or promote products derived from
     this software without specific prior written permission. 
 (d) The text of the required copyright notice is: 
       Licensed Materials - Property of IBM
       DB2 Storage Engine Enablement 
       Copyright IBM Corporation 2007,2008 
       All rights reserved

THIS SOFTWARE IS PROVIDED BY IBM CORPORATION "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL IBM CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/


#ifndef DB2I_BLOBCOLLECTION_H
#define DB2I_BLOBCOLLECTION_H

#include "db2i_global.h"
#include "db2i_file.h"

/** 
   @class ProtectedBuffer
   @brief Implements memory management for (optionally) protected buffers.

   Buffers created with the protection option will have a guard page set on the
   page following requested allocation size. The side effect is that the actual
   allocation is up to 2*4096-1 bytes larger than the size requested by the 
   using code.
*/

class ProtectedBuffer
{
public:
  ProtectedBuffer() : protectBuf(false)
  {;}
  
  void Malloc(size_t size, bool protect = false)
  {
    protectBuf = protect;
    bufptr.alloc(size + (protectBuf ? 0x1fff : 0x0));
    if ((void*)bufptr != NULL)
    {
      len = size;
      if (protectBuf) 
        mprotect(protectedPage(), 0x1000, PROT_NONE);
#ifndef DBUG_OFF
      // Prevents a problem with DBUG_PRINT over-reading in recent versions of 
      // MySQL
      *((char*)protectedPage()-1) = 0;
#endif
    }
  }
  
  void Free()
  {
    if ((void*)bufptr != NULL)
    {
      if (protectBuf)  
        mprotect(protectedPage(), 0x1000, PROT_READ | PROT_WRITE);
      bufptr.dealloc();
    }
  }
  
  ~ProtectedBuffer()
  {
    Free();
  }
  
  ValidatedPointer<char>& ptr()  {return bufptr;}
  bool isProtected() const {return protectBuf;}
  size_t allocLen() const {return len;}
private:
  void* protectedPage()
  {
    return (void*)(((address64_t)(void*)bufptr + len + 0x1000) & ~0xfff);
  }
    
  ValidatedPointer<char> bufptr;
  size_t len;
  bool protectBuf;
  
};


/**
   @class BlobCollection
   @brief Manages memory allocation for reading blobs associated with a table. 
   
   Allocations are done on-demand and are protected with a guard page if less
   than the max possible size is allocated.
*/
class BlobCollection
{
  public: 
  BlobCollection(db2i_table* db2Table, uint32 defaultAllocSize) : 
      defaultAllocation(defaultAllocSize), table(db2Table)
  {
    buffers = new ProtectedBuffer[table->getBlobCount()];
  }

  ~BlobCollection()
  {
    delete[] buffers;
  }
    
  ValidatedPointer<char>& getBufferPtr(int fieldIndex)
  {
    int blobIndex = table->getBlobIdFromField(fieldIndex);
    if ((char*)buffers[blobIndex].ptr() == NULL)
      generateBuffer(fieldIndex);
    
    return buffers[blobIndex].ptr();
  }

  ValidatedPointer<char>& reallocBuffer(int fieldIndex, size_t size);
    
  
  private: 
      
  uint32 getSizeToAllocate(int fieldIndex, bool& shouldProtect);      
  void generateBuffer(int fieldIndex);
  
  db2i_table* table;                            // The table being read
  ProtectedBuffer* buffers;                     // The buffers
  uint32 defaultAllocation;                     
    /* The default size to use when first allocating a buffer */
};

#endif