~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ibmdb2i/db2i_blobCollection.h

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Licensed Materials - Property of IBM
 
3
DB2 Storage Engine Enablement
 
4
Copyright IBM Corporation 2007,2008
 
5
All rights reserved
 
6
 
 
7
Redistribution and use in source and binary forms, with or without modification,
 
8
are permitted provided that the following conditions are met: 
 
9
 (a) Redistributions of source code must retain this list of conditions, the
 
10
     copyright notice in section {d} below, and the disclaimer following this
 
11
     list of conditions. 
 
12
 (b) Redistributions in binary form must reproduce this list of conditions, the
 
13
     copyright notice in section (d) below, and the disclaimer following this
 
14
     list of conditions, in the documentation and/or other materials provided
 
15
     with the distribution. 
 
16
 (c) The name of IBM may not be used to endorse or promote products derived from
 
17
     this software without specific prior written permission. 
 
18
 (d) The text of the required copyright notice is: 
 
19
       Licensed Materials - Property of IBM
 
20
       DB2 Storage Engine Enablement 
 
21
       Copyright IBM Corporation 2007,2008 
 
22
       All rights reserved
 
23
 
 
24
THIS SOFTWARE IS PROVIDED BY IBM CORPORATION "AS IS" AND ANY EXPRESS OR IMPLIED
 
25
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
26
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 
27
SHALL IBM CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 
29
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
30
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
31
CONTRACT, STRICT LIABILITY, OR TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 
32
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 
33
OF SUCH DAMAGE.
 
34
*/
 
35
 
 
36
 
 
37
#ifndef DB2I_BLOBCOLLECTION_H
 
38
#define DB2I_BLOBCOLLECTION_H
 
39
 
 
40
#include "db2i_global.h"
 
41
#include "db2i_file.h"
 
42
 
 
43
/** 
 
44
   @class ProtectedBuffer
 
45
   @brief Implements memory management for (optionally) protected buffers.
 
46
 
 
47
   Buffers created with the protection option will have a guard page set on the
 
48
   page following requested allocation size. The side effect is that the actual
 
49
   allocation is up to 2*4096-1 bytes larger than the size requested by the 
 
50
   using code.
 
51
*/
 
52
 
 
53
class ProtectedBuffer
 
54
{
 
55
public:
 
56
  ProtectedBuffer() : protectBuf(false)
 
57
  {;}
 
58
  
 
59
  void Malloc(size_t size, bool protect = false)
 
60
  {
 
61
    protectBuf = protect;
 
62
    bufptr.alloc(size + (protectBuf ? 0x1fff : 0x0));
 
63
    if ((void*)bufptr != NULL)
 
64
    {
 
65
      len = size;
 
66
      if (protectBuf) 
 
67
        mprotect(protectedPage(), 0x1000, PROT_NONE);
 
68
#ifndef DBUG_OFF
 
69
      // Prevents a problem with DBUG_PRINT over-reading in recent versions of 
 
70
      // MySQL
 
71
      *((char*)protectedPage()-1) = 0;
 
72
#endif
 
73
    }
 
74
  }
 
75
  
 
76
  void Free()
 
77
  {
 
78
    if ((void*)bufptr != NULL)
 
79
    {
 
80
      if (protectBuf)  
 
81
        mprotect(protectedPage(), 0x1000, PROT_READ | PROT_WRITE);
 
82
      bufptr.dealloc();
 
83
    }
 
84
  }
 
85
  
 
86
  ~ProtectedBuffer()
 
87
  {
 
88
    Free();
 
89
  }
 
90
  
 
91
  ValidatedPointer<char>& ptr()  {return bufptr;}
 
92
  bool isProtected() const {return protectBuf;}
 
93
  size_t allocLen() const {return len;}
 
94
private:
 
95
  void* protectedPage()
 
96
  {
 
97
    return (void*)(((address64_t)(void*)bufptr + len + 0x1000) & ~0xfff);
 
98
  }
 
99
    
 
100
  ValidatedPointer<char> bufptr;
 
101
  size_t len;
 
102
  bool protectBuf;
 
103
  
 
104
};
 
105
 
 
106
 
 
107
/**
 
108
   @class BlobCollection
 
109
   @brief Manages memory allocation for reading blobs associated with a table. 
 
110
   
 
111
   Allocations are done on-demand and are protected with a guard page if less
 
112
   than the max possible size is allocated.
 
113
*/
 
114
class BlobCollection
 
115
{
 
116
  public: 
 
117
  BlobCollection(db2i_table* db2Table, uint32 defaultAllocSize) : 
 
118
      defaultAllocation(defaultAllocSize), table(db2Table)
 
119
  {
 
120
    buffers = new ProtectedBuffer[table->getBlobCount()];
 
121
  }
 
122
 
 
123
  ~BlobCollection()
 
124
  {
 
125
    delete[] buffers;
 
126
  }
 
127
    
 
128
  ValidatedPointer<char>& getBufferPtr(int fieldIndex)
 
129
  {
 
130
    int blobIndex = table->getBlobIdFromField(fieldIndex);
 
131
    if ((char*)buffers[blobIndex].ptr() == NULL)
 
132
      generateBuffer(fieldIndex);
 
133
    
 
134
    return buffers[blobIndex].ptr();
 
135
  }
 
136
 
 
137
  ValidatedPointer<char>& reallocBuffer(int fieldIndex, size_t size);
 
138
    
 
139
  
 
140
  private: 
 
141
      
 
142
  uint32 getSizeToAllocate(int fieldIndex, bool& shouldProtect);      
 
143
  void generateBuffer(int fieldIndex);
 
144
  
 
145
  db2i_table* table;                            // The table being read
 
146
  ProtectedBuffer* buffers;                     // The buffers
 
147
  uint32 defaultAllocation;                     
 
148
    /* The default size to use when first allocating a buffer */
 
149
};
 
150
 
 
151
#endif