File: memory.c

package info (click to toggle)
ocfs2-tools 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 6,232 kB
  • sloc: ansic: 86,865; sh: 5,781; python: 2,380; makefile: 1,305
file content (135 lines) | stat: -rw-r--r-- 2,972 bytes parent folder | download
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
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * memory.c
 *
 * Memory routines for the OCFS2 userspace library.
 *
 * Copyright (C) 2004 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License, version 2,  as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301 USA.
 *
 * Portions of this code from e2fsprogs/lib/ext2fs/ext2fs.h
 *  Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
 *  	2002 by Theodore Ts'o.
 */

#define _XOPEN_SOURCE 600  /* Triggers XOPEN2K in features.h */
#define _LARGEFILE64_SOURCE

#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include "ocfs2/ocfs2.h"

errcode_t ocfs2_malloc(unsigned long size, void *ptr)
{
    void **pp = (void **)ptr;

    *pp = malloc(size);
    if (!*pp)
        return OCFS2_ET_NO_MEMORY;
    return 0;
}

errcode_t ocfs2_malloc0(unsigned long size, void *ptr)
{
	errcode_t ret;
	void **pp = (void **)ptr;

	ret = ocfs2_malloc(size, ptr);
	if (ret)
		return ret;

	memset(*pp, 0, size);
	return 0;
}

errcode_t ocfs2_free(void *ptr)
{
	void **pp = (void **)ptr;

	free(*pp);
	*pp = NULL;
	return 0;
}

errcode_t ocfs2_realloc(unsigned long size, void *ptr)
{
	void *p;
	void **pp = (void **)ptr;

	p = realloc(*pp, size);
	if (!p)
		return OCFS2_ET_NO_MEMORY;
	*pp = p;
	return 0;
}

errcode_t ocfs2_realloc0(unsigned long size, void *ptr,
			 unsigned long old_size)
{
	errcode_t ret;
	char *p;
	void **pp = (void **)ptr;

	ret = ocfs2_realloc(size, ptr);
	if (ret)
		return ret;

	if (size > old_size) {
		p = (char *)(*pp);
		memset(p + old_size, 0, size - old_size);
	}
	return 0;
}

errcode_t ocfs2_malloc_blocks(io_channel *channel, int num_blocks,
			      void *ptr)
{
	errcode_t ret;
	int blksize;
	size_t bytes;
	void **pp = (void **)ptr;
	void *tmp;

	blksize = io_get_blksize(channel);
	if (((unsigned long long)num_blocks * blksize) > SIZE_MAX)
		return OCFS2_ET_NO_MEMORY;
	bytes = (unsigned long long)num_blocks * blksize;

	/*
	 * Older glibcs abort when they can't memalign() something.
	 * Ugh!  Check with malloc() first.
	 */
	tmp = malloc(bytes);
	if (!tmp)
		return OCFS2_ET_NO_MEMORY;
	free(tmp);

	ret = posix_memalign(pp, blksize, bytes);
	if (!ret)
		return 0;
	if (errno == ENOMEM)
		return OCFS2_ET_NO_MEMORY;
	/* blksize better be valid */
	abort();
}

errcode_t ocfs2_malloc_block(io_channel *channel, void *ptr)
{
	return ocfs2_malloc_blocks(channel, 1, ptr);
}