amath  1.8.5
Simple command line calculator
memcpy.c File Reference

Copy a block of memory, handling overlap. More...

#include "amathc.h"
Include dependency graph for memcpy.c:

Go to the source code of this file.

Macros

#define TLOOP(s)   if (t) TLOOP1(s)
 loop-t-times More...
 
#define TLOOP1(s)   do { s; } while (--t)
 loop-t-times, t>0 More...
 

Typedefs

typedef uint32_t word
 sizeof(word) MUST BE A POWER OF TWO SO THAT wmask BELOW IS ALL ONES More...
 
typedef uintptr_t mem_ptr
 

Functions

void MemCopy (void *destination, const void *source, unsigned int length)
 Copy a block of memory, handling overlap. More...
 

Detailed Description

Copy a block of memory, handling overlap.

Code originate from FreeBSD base, revision 229286.

The original source code can be obtained from: https://svnweb.freebsd.org/base/head/lib/libc/string/bcopy.c?revision=229286

Definition in file memcpy.c.

Macro Definition Documentation

◆ TLOOP

#define TLOOP (   s)    if (t) TLOOP1(s)

loop-t-times

Definition at line 65 of file memcpy.c.

◆ TLOOP1

#define TLOOP1 (   s)    do { s; } while (--t)

loop-t-times, t>0

Definition at line 70 of file memcpy.c.

Typedef Documentation

◆ mem_ptr

typedef uintptr_t mem_ptr

Definition at line 60 of file memcpy.c.

◆ word

typedef uint32_t word

sizeof(word) MUST BE A POWER OF TWO SO THAT wmask BELOW IS ALL ONES

Definition at line 59 of file memcpy.c.

Function Documentation

◆ MemCopy()

void MemCopy ( void *  destination,
const void *  source,
unsigned int  length 
)

Copy a block of memory, handling overlap.

Definition at line 75 of file memcpy.c.

Referenced by CharBuffer::EnsureSize(), Lexer::GetDigitValue(), Lexer::GetLiteral(), Lexer::GetQuotedIdent(), DecimalSystem::GetText(), and Language::UntagText().

76 {
77  char* dst = (char*) destination;
78  const char* src = (const char*) source;
79  unsigned int t;
80 
81  if (length == 0 || dst == src) // nothing to do
82  return;
83 
84  if ((mem_ptr)dst < (mem_ptr)src)
85  {
86  // Copy forward
87 #if defined(AMIGA) // Take advantage of exec
88  CopyMem((void*)source, destination, length);
89 #else
90  t = (mem_ptr)src; // only need low bits
91  if ((t | (mem_ptr)dst) & wmask)
92  {
93  // Try to align operands. This cannot be done unless the low bits match.
94  if ((t ^ (mem_ptr)dst) & wmask || length < wsize)
95  t = length;
96  else
97  t = wsize - (t & wmask);
98  length -= t;
99 
100  TLOOP1(*dst++ = *src++);
101  }
102 
103  // Copy whole words, then mop up any trailing bytes.
104  t = length / wsize;
105  TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
106 
107  t = length & wmask;
108  TLOOP(*dst++ = *src++);
109 #endif
110  }
111  else
112  {
113  // Copy backwards. Otherwise essentially the same.
114  // Alignment works as before, except that it takes
115  // (t&wmask) bytes to align, not wsize-(t&wmask).
116  src += length;
117  dst += length;
118  t = (mem_ptr)src;
119  if ((t | (mem_ptr)dst) & wmask)
120  {
121  if ((t ^ (mem_ptr)dst) & wmask || length <= wsize)
122  t = length;
123  else
124  t &= wmask;
125  length -= t;
126 
127  TLOOP1(*--dst = *--src);
128  }
129 
130  t = length / wsize;
131  TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
132 
133  t = length & wmask;
134  TLOOP(*--dst = *--src);
135  }
136 }
#define wmask
Definition: amath.h:233
#define TLOOP1(s)
loop-t-times, t>0
Definition: memcpy.c:70
uint32_t word
sizeof(word) MUST BE A POWER OF TWO SO THAT wmask BELOW IS ALL ONES
Definition: memcpy.c:59
#define wsize
Definition: amath.h:232
uintptr_t mem_ptr
Definition: memcpy.c:60
#define TLOOP(s)
loop-t-times
Definition: memcpy.c:65
Here is the caller graph for this function: