Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Functions
MemoryFunctions.cpp File Reference

implements of function which deals with memory More...

#include "stdafx.h"
#include "MemoryFunctions.h"
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for MemoryFunctions.cpp:

Go to the source code of this file.

Functions

void fillarray (void *p, void *value, size_t size, size_t n)
 This function assigns the specified value to each element. More...
 
void fillzero (void *p, size_t n)
 This function fills a block of memory zeros. More...
 
bool isbigendian ()
 judges whether the system is big endian More...
 
bool islittleendian ()
 gets the system is little endian More...
 
void memreverse (void *p, size_t n)
 reverse memory (ex. Big Endian <-> Little Endian) More...
 
void memswap (void *p0, void *p1, size_t n)
 swaps memory ( p0 <-> p1 ) More...
 

Detailed Description

implements of function which deals with memory

Author
S.Tanaka
Date
2006.07.13

Copyright(C) 2006-2014 Eisai Co., Ltd. All rights reserved.

Definition in file MemoryFunctions.cpp.

Function Documentation

void fillarray ( void *  p,
void *  value,
size_t  size,
size_t  n 
)

This function assigns the specified value to each element.

Parameters
[out]parray
[in]valuespecified value
[in]sizesize of element
[in]nsize of array

Definition at line 20 of file MemoryFunctions.cpp.

20  {
21  // void* -> char* (because void* doesn't have the size of element)
22  char* cp = (char*)p;
23  char* cv = (char*)value;
24 
25  // fill
26  for( unsigned int i = 0; i < n; i++ ) {
27  char* p_i = cp + ( size * i );
28  for( unsigned int j = 0; j < size; j++ ) {
29  p_i[ j ] = cv[ j ];
30  }
31  }
32 }
void fillzero ( void *  p,
size_t  n 
)

This function fills a block of memory zeros.

Parameters
[out]ppointer to starting addres of the block of memory to fill with zeros.
[in]nsize of the block of memory to fill with zeros.

Definition at line 35 of file MemoryFunctions.cpp.

35  {
36  unsigned char zero = 0;
37 
38  fillarray( p, &zero, 1, n );
39 }
void fillarray(void *p, void *value, size_t size, size_t n)
This function assigns the specified value to each element.

Here is the call graph for this function:

bool isbigendian ( )

judges whether the system is big endian

Returns
If the system is big endian, this function returns true.

Definition at line 42 of file MemoryFunctions.cpp.

42  {
43  // create 2 bytes data
44  short num = 1; // (Big: 00 01, Little: 01 00)
45 
46  // get first byte
47  char* c = (char*)&num;
48 
49  return ( *c == 0 );
50 }
bool islittleendian ( )

gets the system is little endian

Returns
If the system is little endian, this function returns true.

Definition at line 53 of file MemoryFunctions.cpp.

53  {
54  return !isbigendian();
55 }
bool isbigendian()
judges whether the system is big endian

Here is the call graph for this function:

void memreverse ( void *  p,
size_t  n 
)

reverse memory (ex. Big Endian <-> Little Endian)

Parameters
[in,out]pthe address of memory
[in]nmemory size

Definition at line 58 of file MemoryFunctions.cpp.

58  {
59  // check parameters
60  if( p == NULL || n == 0 ) {
61  return;
62  }
63 
64  // create buffer
65  char* buffer = new char[ n ];
66 
67  // copy to buffer
68  memcpy( buffer, p, n );
69 
70  // reverse
71  char* cp = (char*)p;
72  for( unsigned int i = 0; i < n; i++ ) {
73  cp[ i ] = buffer[ n - 1 - i ];
74  }
75 
76  // delete
77  delete[] buffer;
78 }
#define NULL
Definition: CoreMacros.h:18
void memswap ( void *  p0,
void *  p1,
size_t  n 
)

swaps memory ( p0 <-> p1 )

Parameters
[in,out]p0the address of memory to be swapped
[in,out]p1the address of memory to swap
[in]nmemory size

Definition at line 81 of file MemoryFunctions.cpp.

81  {
82  // check parameters
83  if( p0 == NULL || p1 == NULL || n == 0 ) {
84  return;
85  }
86 
87  // create buffer
88  void* tmp = malloc( n );
89 
90  // copy
91  memcpy( tmp, p0, n );
92  memcpy( p0, p1, n );
93  memcpy( p1, tmp, n );
94 
95  // free
96  free( tmp );
97 }
#define NULL
Definition: CoreMacros.h:18