Coverage Report

Created: 2025-06-13 07:09

/src/server/mysys/my_once.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
15
16
/* Not MT-SAFE */
17
18
#include "mysys_priv.h"
19
#include "my_static.h"
20
#include "mysys_err.h"
21
#include <m_string.h>
22
#include <my_atomic.h>
23
24
int64 my_malloc_init_memory_allocated= 0;
25
26
/*
27
  Alloc for things we don't nend to free run-time (that only
28
  should be free'd on exit)
29
30
  SYNOPSIS
31
    my_once_alloc()
32
      Size
33
      MyFlags
34
35
  NOTES
36
    No DBUG_ENTER... here to get smaller dbug-startup 
37
*/
38
39
void* my_once_alloc(size_t Size, myf MyFlags)
40
0
{
41
0
  size_t get_size, max_left;
42
0
  uchar* point;
43
0
  reg1 USED_MEM *next;
44
0
  reg2 USED_MEM **prev;
45
46
0
  Size= ALIGN_SIZE(Size);
47
0
  prev= &my_once_root_block;
48
0
  max_left=0;
49
0
  for (next=my_once_root_block ; next && next->left < Size ; next= next->next)
50
0
  {
51
0
    if (next->left > max_left)
52
0
      max_left=next->left;
53
0
    prev= &next->next;
54
0
  }
55
0
  if (! next)
56
0
  {           /* Time to alloc new block */
57
0
    get_size= Size+ALIGN_SIZE(sizeof(USED_MEM));
58
0
    if (max_left*4 < my_once_extra && get_size < my_once_extra)
59
0
      get_size=my_once_extra;     /* Normal alloc */
60
61
0
    if ((next = (USED_MEM*) malloc(get_size)) == 0)
62
0
    {
63
0
      my_errno=errno;
64
0
      if (MyFlags & (MY_FAE+MY_WME))
65
0
  my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_FATAL), get_size);
66
0
      return((uchar*) 0);
67
0
    }
68
0
    my_atomic_add64_explicit(&my_malloc_init_memory_allocated,
69
0
                             (int64) get_size,
70
0
                             MY_MEMORY_ORDER_RELAXED);
71
0
    DBUG_PRINT("test",("my_once_malloc %lu byte malloced", (ulong) get_size));
72
0
    next->next= 0;
73
0
    next->size= get_size;
74
0
    next->left= get_size-ALIGN_SIZE(sizeof(USED_MEM));
75
0
    *prev=next;
76
0
  }
77
0
  point= (uchar*) ((char*) next+ (next->size-next->left));
78
0
  next->left-= Size;
79
80
0
  if (MyFlags & MY_ZEROFILL)
81
0
    bzero(point, Size);
82
0
  return((void*) point);
83
0
} /* my_once_alloc */
84
85
86
char *my_once_strdup(const char *src,myf myflags)
87
0
{
88
0
  size_t len= strlen(src)+1;
89
0
  uchar *dst= my_once_alloc(len, myflags);
90
0
  if (dst)
91
0
    memcpy(dst, src, len);
92
0
  return (char*) dst;
93
0
}
94
95
96
void *my_once_memdup(const void *src, size_t len, myf myflags)
97
0
{
98
0
  uchar *dst= my_once_alloc(len, myflags);
99
0
  if (dst)
100
0
    memcpy(dst, src, len);
101
0
  return dst;
102
0
}
103
104
105
/*
106
  Deallocate everything that was allocated with my_once_alloc
107
108
  SYNOPSIS
109
    my_once_free()
110
*/
111
112
void my_once_free(void)
113
0
{
114
0
  reg1 USED_MEM *next,*old;
115
0
  DBUG_ENTER("my_once_free");
116
117
0
  for (next=my_once_root_block ; next ; )
118
0
  {
119
0
    old=next; next= next->next ;
120
0
    free((uchar*) old);
121
0
  }
122
0
  my_once_root_block=0;
123
0
  my_malloc_init_memory_allocated= 0;
124
125
0
  DBUG_VOID_RETURN;
126
0
} /* my_once_free */