Skip to content

Commit

Permalink
mdadm: add xmalloc.h
Browse files Browse the repository at this point in the history
Move memory declaration helpers outside mdadm.h. They seems to be
useful so keep them but include separatelly. Rework them to not reffer
to Name[] declared internally in mdadm/mdmon.

This is first step to start decomplexing mdadm.h.

Signed-off-by: Mariusz Tkaczyk <[email protected]>
  • Loading branch information
mtkaczyk committed Sep 27, 2024
1 parent 3446903 commit ee3a6ca
Show file tree
Hide file tree
Showing 33 changed files with 100 additions and 44 deletions.
2 changes: 2 additions & 0 deletions Assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/

#include "mdadm.h"
#include "xmalloc.h"

#include <ctype.h>

mapping_t assemble_statuses[] = {
Expand Down
4 changes: 3 additions & 1 deletion Create.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
*/

#include "mdadm.h"
#include "udev.h"
#include "md_u.h"
#include "md_p.h"
#include "udev.h"
#include "xmalloc.h"

#include <ctype.h>
#include <fcntl.h>
#include <signal.h>
Expand Down
2 changes: 2 additions & 0 deletions Detail.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "mdadm.h"
#include "md_p.h"
#include "md_u.h"
#include "xmalloc.h"

#include <ctype.h>
#include <dirent.h>

Expand Down
8 changes: 5 additions & 3 deletions Examine.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
* Email: <[email protected]>
*/

#include "mdadm.h"
#include "dlink.h"
#include "mdadm.h"
#include "md_u.h"
#include "md_p.h"
#include "xmalloc.h"

#if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
#error no endian defined
#endif
#include "md_u.h"
#include "md_p.h"

int Examine(struct mddev_dev *devlist,
struct context *c,
struct supertype *forcest)
Expand Down
2 changes: 2 additions & 0 deletions Grow.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
#include "mdadm.h"
#include "dlink.h"
#include "xmalloc.h"

#include <sys/mman.h>
#include <stddef.h>
#include <stdint.h>
Expand Down
2 changes: 2 additions & 0 deletions Incremental.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/

#include "mdadm.h"
#include "xmalloc.h"

#include <sys/wait.h>
#include <dirent.h>
#include <ctype.h>
Expand Down
2 changes: 2 additions & 0 deletions Manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "md_u.h"
#include "md_p.h"
#include "udev.h"
#include "xmalloc.h"

#include <ctype.h>

int Manage_ro(char *devname, int fd, int readonly)
Expand Down
4 changes: 3 additions & 1 deletion Monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
*/

#include "mdadm.h"
#include "udev.h"
#include "md_p.h"
#include "md_u.h"
#include "udev.h"
#include "xmalloc.h"

#include <sys/wait.h>
#include <limits.h>
#include <syslog.h>
Expand Down
1 change: 1 addition & 0 deletions bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include "mdadm.h"
#include "xmalloc.h"

static inline void sb_le_to_cpu(bitmap_super_t *sb)
{
Expand Down
2 changes: 2 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include "mdadm.h"
#include "dlink.h"
#include "xmalloc.h"

#include <dirent.h>
#include <glob.h>
#include <fnmatch.h>
Expand Down
2 changes: 2 additions & 0 deletions lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include "mdadm.h"
#include "dlink.h"
#include "xmalloc.h"

#include <ctype.h>
#include <limits.h>

Expand Down
2 changes: 2 additions & 0 deletions managemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
#endif
#include "mdadm.h"
#include "mdmon.h"
#include "xmalloc.h"

#include <sys/syscall.h>
#include <sys/socket.h>

Expand Down
2 changes: 2 additions & 0 deletions mapfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
* at compile time via MAP_DIR and MAP_FILE.
*/
#include "mdadm.h"
#include "xmalloc.h"

#include <sys/file.h>
#include <ctype.h>

Expand Down
2 changes: 2 additions & 0 deletions mdadm.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include "mdadm.h"
#include "md_p.h"
#include "xmalloc.h"

#include <ctype.h>

/**
Expand Down
5 changes: 0 additions & 5 deletions mdadm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1937,11 +1937,6 @@ static inline int xasprintf(char **strp, const char *fmt, ...) {

#define pr_vrb(fmt, arg...) ((void)(verbose && pr_err(fmt, ##arg)))

void *xmalloc(size_t len);
void *xrealloc(void *ptr, size_t len);
void *xcalloc(size_t num, size_t size);
char *xstrdup(const char *str);

#define LEVEL_MULTIPATH (-4)
#define LEVEL_LINEAR (-1)
#define LEVEL_FAULTY (-5)
Expand Down
1 change: 1 addition & 0 deletions mdmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

#include "mdadm.h"
#include "mdmon.h"
#include "xmalloc.h"

char const Name[] = "mdmon";

Expand Down
2 changes: 2 additions & 0 deletions mdopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "mdadm.h"
#include "udev.h"
#include "md_p.h"
#include "xmalloc.h"

#include <ctype.h>

void make_parts(char *dev, int cnt)
Expand Down
2 changes: 2 additions & 0 deletions mdstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@

#include "mdadm.h"
#include "dlink.h"
#include "xmalloc.h"

#include <sys/select.h>
#include <ctype.h>

Expand Down
1 change: 1 addition & 0 deletions msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <sys/un.h>
#include "mdadm.h"
#include "mdmon.h"
#include "xmalloc.h"

static const __u32 start_magic = 0x5a5aa5a5;
static const __u32 end_magic = 0xa5a55a5a;
Expand Down
2 changes: 2 additions & 0 deletions platform-intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "mdadm.h"
#include "platform-intel.h"
#include "probe_roms.h"
#include "xmalloc.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down
2 changes: 2 additions & 0 deletions policy.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/

#include "mdadm.h"
#include "xmalloc.h"

#include <dirent.h>
#include <fnmatch.h>
#include <ctype.h>
Expand Down
2 changes: 2 additions & 0 deletions restripe.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/

#include "mdadm.h"
#include "xmalloc.h"

#include <stdint.h>

/* To restripe, we read from old geometry to a buffer, and
Expand Down
2 changes: 2 additions & 0 deletions super-ddf.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "mdadm.h"
#include "mdmon.h"
#include "sha1.h"
#include "xmalloc.h"

#include <values.h>
#include <stddef.h>

Expand Down
1 change: 1 addition & 0 deletions super-gpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include "mdadm.h"
#include "part.h"
#include "xmalloc.h"

static void free_gpt(struct supertype *st)
{
Expand Down
2 changes: 2 additions & 0 deletions super-intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "dlink.h"
#include "sha1.h"
#include "platform-intel.h"
#include "xmalloc.h"

#include <values.h>
#include <scsi/sg.h>
#include <ctype.h>
Expand Down
1 change: 1 addition & 0 deletions super-mbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include "mdadm.h"
#include "part.h"
#include "xmalloc.h"

static void free_mbr(struct supertype *st)
{
Expand Down
2 changes: 2 additions & 0 deletions super0.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#define HAVE_STDINT_H 1
#include "mdadm.h"
#include "sha1.h"
#include "xmalloc.h"

/*
* All handling for the 0.90.0 version superblock is in
* this file.
Expand Down
2 changes: 2 additions & 0 deletions super1.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <stddef.h>
#include "mdadm.h"
#include "xmalloc.h"

/*
* The version-1 superblock :
* All numeric fields are little-endian.
Expand Down
4 changes: 3 additions & 1 deletion sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
*/

#include "mdadm.h"
#include "dlink.h"
#include "xmalloc.h"

#include <dirent.h>
#include <ctype.h>
#include "dlink.h"

#define MAX_SYSFS_PATH_LEN 120

Expand Down
2 changes: 2 additions & 0 deletions udev.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "udev.h"
#include "md_p.h"
#include "md_u.h"
#include "xmalloc.h"

#include <sys/wait.h>
#include <signal.h>
#include <limits.h>
Expand Down
2 changes: 2 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include "mdadm.h"
#include "md_p.h"
#include "xmalloc.h"

#include <sys/socket.h>
#include <sys/utsname.h>
#include <sys/wait.h>
Expand Down
59 changes: 26 additions & 33 deletions xmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,57 @@
* Email: <[email protected]>
*/

#include "mdadm.h"
/*#include <sys/socket.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <ctype.h>
#include <dirent.h>
#include <signal.h>
*/
#include "xmalloc.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void *exit_memory_alloc_failure(void)
{
fprintf(stderr, "Memory allocation failure - aborting\n");

/* TODO: replace with MDADM_STATUS_MEM_FAIL */
exit(1);
}

void *xmalloc(size_t len)
{
void *rv = malloc(len);
char *msg;
int n;

if (rv)
return rv;
msg = ": memory allocation failure - aborting\n";
n = write(2, Name, strlen(Name));
n += write(2, msg, strlen(msg));
exit(4+!!n);

return exit_memory_alloc_failure();
}

void *xrealloc(void *ptr, size_t len)
{
void *rv = realloc(ptr, len);
char *msg;
int n;

if (rv)
return rv;
msg = ": memory allocation failure - aborting\n";
n = write(2, Name, strlen(Name));
n += write(2, msg, strlen(msg));
exit(4+!!n);

return exit_memory_alloc_failure();
}

void *xcalloc(size_t num, size_t size)
{
void *rv = calloc(num, size);
char *msg;
int n;

if (rv)
return rv;
msg = ": memory allocation failure - aborting\n";
n = write(2, Name, strlen(Name));
n += write(2, msg, strlen(msg));
exit(4+!!n);

return exit_memory_alloc_failure();
}

char *xstrdup(const char *str)
{
char *rv = strdup(str);
char *msg;
int n;

if (rv)
return rv;
msg = ": memory allocation failure - aborting\n";
n = write(2, Name, strlen(Name));
n += write(2, msg, strlen(msg));
exit(4+!!n);

return exit_memory_alloc_failure();
}
Loading

0 comments on commit ee3a6ca

Please sign in to comment.