~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/mem/mem0pool.c

  • Committer: Brian Aker
  • Date: 2008-12-23 07:19:26 UTC
  • Revision ID: brian@tangent.org-20081223071926-69z2ugpftfz1lfnm
Remove dead variables.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (c) 1997, 2009, Innobase Oy. All Rights Reserved.
4
 
 
5
 
This program is free software; you can redistribute it and/or modify it under
6
 
the terms of the GNU General Public License as published by the Free Software
7
 
Foundation; version 2 of the License.
8
 
 
9
 
This program is distributed in the hope that it will be useful, but WITHOUT
10
 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
 
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
 
 
13
 
You should have received a copy of the GNU General Public License along with
14
 
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
 
St, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
*****************************************************************************/
18
 
 
19
 
/********************************************************************//**
20
 
@file mem/mem0pool.c
 
1
/************************************************************************
21
2
The lowest-level memory management
22
3
 
 
4
(c) 1997 Innobase Oy
 
5
 
23
6
Created 5/12/1997 Heikki Tuuri
24
7
*************************************************************************/
25
8
 
28
11
#include "mem0pool.ic"
29
12
#endif
30
13
 
31
 
#include "srv0srv.h"
32
14
#include "sync0sync.h"
33
15
#include "ut0mem.h"
34
16
#include "ut0lst.h"
35
17
#include "ut0byte.h"
36
18
#include "mem0mem.h"
37
 
#include "srv0start.h"
38
19
 
39
20
/* We would like to use also the buffer frames to allocate memory. This
40
21
would be desirable, because then the memory consumption of the database
91
72
can occupy at most about the size of the buffer frame of memory in the common
92
73
pool, and after that its locks will grow into the buffer pool. */
93
74
 
94
 
/** Mask used to extract the free bit from area->size */
 
75
/* Mask used to extract the free bit from area->size */
95
76
#define MEM_AREA_FREE   1
96
77
 
97
 
/** The smallest memory area total size */
 
78
/* The smallest memory area total size */
98
79
#define MEM_AREA_MIN_SIZE       (2 * MEM_AREA_EXTRA_SIZE)
99
80
 
100
81
 
101
 
/** Data structure for a memory pool. The space is allocated using the buddy
 
82
/* Data structure for a memory pool. The space is allocated using the buddy
102
83
algorithm, where free list i contains areas of size 2 to power i. */
103
84
struct mem_pool_struct{
104
 
        byte*           buf;            /*!< memory pool */
105
 
        ulint           size;           /*!< memory common pool size */
106
 
        ulint           reserved;       /*!< amount of currently allocated
 
85
        byte*           buf;            /* memory pool */
 
86
        ulint           size;           /* memory common pool size */
 
87
        ulint           reserved;       /* amount of currently allocated
107
88
                                        memory */
108
 
        mutex_t         mutex;          /*!< mutex protecting this struct */
 
89
        mutex_t         mutex;          /* mutex protecting this struct */
109
90
        UT_LIST_BASE_NODE_T(mem_area_t)
110
 
                        free_list[64];  /*!< lists of free memory areas: an
 
91
                        free_list[64];  /* lists of free memory areas: an
111
92
                                        area is put to the list whose number
112
93
                                        is the 2-logarithm of the area size */
113
94
};
114
95
 
115
 
/** The common memory pool */
 
96
/* The common memory pool */
116
97
UNIV_INTERN mem_pool_t* mem_comm_pool   = NULL;
117
98
 
118
 
#ifdef UNIV_PFS_MUTEX
119
 
/* Key to register mutex in mem_pool_struct with performance schema */
120
 
UNIV_INTERN mysql_pfs_key_t     mem_pool_mutex_key;
121
 
#endif /* UNIV_PFS_MUTEX */
122
 
 
123
99
/* We use this counter to check that the mem pool mutex does not leak;
124
100
this is to track a strange assertion failure reported at
125
101
mysql@lists.mysql.com */
126
102
 
127
103
UNIV_INTERN ulint       mem_n_threads_inside            = 0;
128
104
 
129
 
/********************************************************************//**
130
 
Reserves the mem pool mutex if we are not in server shutdown. Use
131
 
this function only in memory free functions, since only memory
132
 
free functions are used during server shutdown. */
133
 
UNIV_INLINE
134
 
void
135
 
mem_pool_mutex_enter(
136
 
/*=================*/
137
 
        mem_pool_t*     pool)           /*!< in: memory pool */
138
 
{
139
 
        if (srv_shutdown_state < SRV_SHUTDOWN_EXIT_THREADS) {
140
 
                mutex_enter(&(pool->mutex));
141
 
        }
142
 
}
143
 
 
144
 
/********************************************************************//**
145
 
Releases the mem pool mutex if we are not in server shutdown. As
146
 
its corresponding mem_pool_mutex_enter() function, use it only
147
 
in memory free functions */
148
 
UNIV_INLINE
149
 
void
150
 
mem_pool_mutex_exit(
151
 
/*================*/
152
 
        mem_pool_t*     pool)           /*!< in: memory pool */
153
 
{
154
 
        if (srv_shutdown_state < SRV_SHUTDOWN_EXIT_THREADS) {
155
 
                mutex_exit(&(pool->mutex));
156
 
        }
157
 
}
158
 
 
159
 
/********************************************************************//**
160
 
Returns memory area size.
161
 
@return size */
 
105
/************************************************************************
 
106
Reserves the mem pool mutex. */
 
107
UNIV_INTERN
 
108
void
 
109
mem_pool_mutex_enter(void)
 
110
/*======================*/
 
111
{
 
112
        mutex_enter(&(mem_comm_pool->mutex));
 
113
}
 
114
 
 
115
/************************************************************************
 
116
Releases the mem pool mutex. */
 
117
UNIV_INTERN
 
118
void
 
119
mem_pool_mutex_exit(void)
 
120
/*=====================*/
 
121
{
 
122
        mutex_exit(&(mem_comm_pool->mutex));
 
123
}
 
124
 
 
125
/************************************************************************
 
126
Returns memory area size. */
162
127
UNIV_INLINE
163
128
ulint
164
129
mem_area_get_size(
165
130
/*==============*/
166
 
        mem_area_t*     area)   /*!< in: area */
 
131
                                /* out: size */
 
132
        mem_area_t*     area)   /* in: area */
167
133
{
168
134
        return(area->size_and_free & ~MEM_AREA_FREE);
169
135
}
170
136
 
171
 
/********************************************************************//**
 
137
/************************************************************************
172
138
Sets memory area size. */
173
139
UNIV_INLINE
174
140
void
175
141
mem_area_set_size(
176
142
/*==============*/
177
 
        mem_area_t*     area,   /*!< in: area */
178
 
        ulint           size)   /*!< in: size */
 
143
        mem_area_t*     area,   /* in: area */
 
144
        ulint           size)   /* in: size */
179
145
{
180
146
        area->size_and_free = (area->size_and_free & MEM_AREA_FREE)
181
147
                | size;
182
148
}
183
149
 
184
 
/********************************************************************//**
185
 
Returns memory area free bit.
186
 
@return TRUE if free */
 
150
/************************************************************************
 
151
Returns memory area free bit. */
187
152
UNIV_INLINE
188
153
ibool
189
154
mem_area_get_free(
190
155
/*==============*/
191
 
        mem_area_t*     area)   /*!< in: area */
 
156
                                /* out: TRUE if free */
 
157
        mem_area_t*     area)   /* in: area */
192
158
{
193
159
#if TRUE != MEM_AREA_FREE
194
160
# error "TRUE != MEM_AREA_FREE"
196
162
        return(area->size_and_free & MEM_AREA_FREE);
197
163
}
198
164
 
199
 
/********************************************************************//**
 
165
/************************************************************************
200
166
Sets memory area free bit. */
201
167
UNIV_INLINE
202
168
void
203
169
mem_area_set_free(
204
170
/*==============*/
205
 
        mem_area_t*     area,   /*!< in: area */
206
 
        ibool           free)   /*!< in: free bit value */
 
171
        mem_area_t*     area,   /* in: area */
 
172
        ibool           free)   /* in: free bit value */
207
173
{
208
174
#if TRUE != MEM_AREA_FREE
209
175
# error "TRUE != MEM_AREA_FREE"
212
178
                | free;
213
179
}
214
180
 
215
 
/********************************************************************//**
216
 
Creates a memory pool.
217
 
@return memory pool */
 
181
/************************************************************************
 
182
Creates a memory pool. */
218
183
UNIV_INTERN
219
184
mem_pool_t*
220
185
mem_pool_create(
221
186
/*============*/
222
 
        ulint   size)   /*!< in: pool size in bytes */
 
187
                        /* out: memory pool */
 
188
        ulint   size)   /* in: pool size in bytes */
223
189
{
224
190
        mem_pool_t*     pool;
225
191
        mem_area_t*     area;
226
192
        ulint           i;
227
193
        ulint           used;
228
194
 
 
195
        ut_a(size > 10000);
 
196
 
229
197
        pool = ut_malloc(sizeof(mem_pool_t));
230
198
 
231
199
        /* We do not set the memory to zero (FALSE) in the pool,
235
203
        pool->buf = ut_malloc_low(size, FALSE, TRUE);
236
204
        pool->size = size;
237
205
 
238
 
        mutex_create(mem_pool_mutex_key, &pool->mutex, SYNC_MEM_POOL);
 
206
        mutex_create(&pool->mutex, SYNC_MEM_POOL);
239
207
 
240
208
        /* Initialize the free lists */
241
209
 
276
244
        return(pool);
277
245
}
278
246
 
279
 
/********************************************************************//**
280
 
Frees a memory pool. */
281
 
UNIV_INTERN
282
 
void
283
 
mem_pool_free(
284
 
/*==========*/
285
 
        mem_pool_t*     pool)   /*!< in, own: memory pool */
286
 
{
287
 
        ut_free(pool->buf);
288
 
        ut_free(pool);
289
 
}
290
 
 
291
 
/********************************************************************//**
292
 
Fills the specified free list.
293
 
@return TRUE if we were able to insert a block to the free list */
 
247
/************************************************************************
 
248
Fills the specified free list. */
294
249
static
295
250
ibool
296
251
mem_pool_fill_free_list(
297
252
/*====================*/
298
 
        ulint           i,      /*!< in: free list index */
299
 
        mem_pool_t*     pool)   /*!< in: memory pool */
 
253
                                /* out: TRUE if we were able to insert a
 
254
                                block to the free list */
 
255
        ulint           i,      /* in: free list index */
 
256
        mem_pool_t*     pool)   /* in: memory pool */
300
257
{
301
258
        mem_area_t*     area;
302
259
        mem_area_t*     area2;
359
316
        return(TRUE);
360
317
}
361
318
 
362
 
/********************************************************************//**
 
319
/************************************************************************
363
320
Allocates memory from a pool. NOTE: This low-level function should only be
364
 
used in mem0mem.*!
365
 
@return own: allocated memory buffer */
 
321
used in mem0mem.*! */
366
322
UNIV_INTERN
367
323
void*
368
324
mem_area_alloc(
369
325
/*===========*/
370
 
        ulint*          psize,  /*!< in: requested size in bytes; for optimum
 
326
                                /* out, own: allocated memory buffer */
 
327
        ulint*          psize,  /* in: requested size in bytes; for optimum
371
328
                                space usage, the size should be a power of 2
372
329
                                minus MEM_AREA_EXTRA_SIZE;
373
330
                                out: allocated size in bytes (greater than
374
331
                                or equal to the requested size) */
375
 
        mem_pool_t*     pool)   /*!< in: memory pool */
 
332
        mem_pool_t*     pool)   /* in: memory pool */
376
333
{
377
334
        mem_area_t*     area;
378
335
        ulint           size;
379
336
        ulint           n;
380
337
        ibool           ret;
381
338
 
382
 
        /* If we are using os allocator just make a simple call
383
 
        to malloc */
384
 
        if (UNIV_LIKELY(srv_use_sys_malloc)) {
385
 
                return(malloc(*psize));
386
 
        }
387
 
 
388
339
        size = *psize;
389
340
        n = ut_2_log(ut_max(size + MEM_AREA_EXTRA_SIZE, MEM_AREA_MIN_SIZE));
390
341
 
463
414
        return((void*)(MEM_AREA_EXTRA_SIZE + ((byte*)area)));
464
415
}
465
416
 
466
 
/********************************************************************//**
467
 
Gets the buddy of an area, if it exists in pool.
468
 
@return the buddy, NULL if no buddy in pool */
 
417
/************************************************************************
 
418
Gets the buddy of an area, if it exists in pool. */
469
419
UNIV_INLINE
470
420
mem_area_t*
471
421
mem_area_get_buddy(
472
422
/*===============*/
473
 
        mem_area_t*     area,   /*!< in: memory area */
474
 
        ulint           size,   /*!< in: memory area size */
475
 
        mem_pool_t*     pool)   /*!< in: memory pool */
 
423
                                /* out: the buddy, NULL if no buddy in pool */
 
424
        mem_area_t*     area,   /* in: memory area */
 
425
        ulint           size,   /* in: memory area size */
 
426
        mem_pool_t*     pool)   /* in: memory pool */
476
427
{
477
428
        mem_area_t*     buddy;
478
429
 
503
454
        return(buddy);
504
455
}
505
456
 
506
 
/********************************************************************//**
 
457
/************************************************************************
507
458
Frees memory to a pool. */
508
459
UNIV_INTERN
509
460
void
510
461
mem_area_free(
511
462
/*==========*/
512
 
        void*           ptr,    /*!< in, own: pointer to allocated memory
 
463
        void*           ptr,    /* in, own: pointer to allocated memory
513
464
                                buffer */
514
 
        mem_pool_t*     pool)   /*!< in: memory pool */
 
465
        mem_pool_t*     pool)   /* in: memory pool */
515
466
{
516
467
        mem_area_t*     area;
517
468
        mem_area_t*     buddy;
519
470
        ulint           size;
520
471
        ulint           n;
521
472
 
522
 
        if (UNIV_LIKELY(srv_use_sys_malloc)) {
523
 
                free(ptr);
524
 
 
525
 
                return;
526
 
        }
527
 
 
528
473
        /* It may be that the area was really allocated from the OS with
529
474
        regular malloc: check if ptr points within our memory pool */
530
475
 
583
528
 
584
529
        n = ut_2_log(size);
585
530
 
586
 
        mem_pool_mutex_enter(pool);
 
531
        mutex_enter(&(pool->mutex));
587
532
        mem_n_threads_inside++;
588
533
 
589
534
        ut_a(mem_n_threads_inside == 1);
611
556
                pool->reserved += ut_2_exp(n);
612
557
 
613
558
                mem_n_threads_inside--;
614
 
                mem_pool_mutex_exit(pool);
 
559
                mutex_exit(&(pool->mutex));
615
560
 
616
561
                mem_area_free(new_ptr, pool);
617
562
 
627
572
        }
628
573
 
629
574
        mem_n_threads_inside--;
630
 
        mem_pool_mutex_exit(pool);
 
575
        mutex_exit(&(pool->mutex));
631
576
 
632
577
        ut_ad(mem_pool_validate(pool));
633
578
}
634
579
 
635
 
/********************************************************************//**
636
 
Validates a memory pool.
637
 
@return TRUE if ok */
 
580
/************************************************************************
 
581
Validates a memory pool. */
638
582
UNIV_INTERN
639
583
ibool
640
584
mem_pool_validate(
641
585
/*==============*/
642
 
        mem_pool_t*     pool)   /*!< in: memory pool */
 
586
                                /* out: TRUE if ok */
 
587
        mem_pool_t*     pool)   /* in: memory pool */
643
588
{
644
589
        mem_area_t*     area;
645
590
        mem_area_t*     buddy;
646
591
        ulint           free;
647
592
        ulint           i;
648
593
 
649
 
        mem_pool_mutex_enter(pool);
 
594
        mutex_enter(&(pool->mutex));
650
595
 
651
596
        free = 0;
652
597
 
653
598
        for (i = 0; i < 64; i++) {
654
599
 
655
 
                UT_LIST_VALIDATE(free_list, mem_area_t, pool->free_list[i],
656
 
                                 (void) 0);
 
600
                UT_LIST_VALIDATE(free_list, mem_area_t, pool->free_list[i]);
657
601
 
658
602
                area = UT_LIST_GET_FIRST(pool->free_list[i]);
659
603
 
674
618
 
675
619
        ut_a(free + pool->reserved == pool->size);
676
620
 
677
 
        mem_pool_mutex_exit(pool);
 
621
        mutex_exit(&(pool->mutex));
678
622
 
679
623
        return(TRUE);
680
624
}
681
625
 
682
 
/********************************************************************//**
 
626
/************************************************************************
683
627
Prints info of a memory pool. */
684
628
UNIV_INTERN
685
629
void
686
630
mem_pool_print_info(
687
631
/*================*/
688
 
        FILE*           outfile,/*!< in: output file to write to */
689
 
        mem_pool_t*     pool)   /*!< in: memory pool */
 
632
        FILE*           outfile,/* in: output file to write to */
 
633
        mem_pool_t*     pool)   /* in: memory pool */
690
634
{
691
635
        ulint           i;
692
636
 
712
656
        mutex_exit(&(pool->mutex));
713
657
}
714
658
 
715
 
/********************************************************************//**
716
 
Returns the amount of reserved memory.
717
 
@return reserved memory in bytes */
 
659
/************************************************************************
 
660
Returns the amount of reserved memory. */
718
661
UNIV_INTERN
719
662
ulint
720
663
mem_pool_get_reserved(
721
664
/*==================*/
722
 
        mem_pool_t*     pool)   /*!< in: memory pool */
 
665
                                /* out: reserved memory in bytes */
 
666
        mem_pool_t*     pool)   /* in: memory pool */
723
667
{
724
668
        ulint   reserved;
725
669