Subversion Repositories HelenOS

Rev

Rev 3386 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3386 Rev 4153
Line 1... Line 1...
1
/*
1
/*
2
 * Copyright (c) 2001-2004 Jakub Jermar
2
 * Copyright (c) 2001-2004 Jakub Jermar
-
 
3
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
4
 * All rights reserved.
4
 *
5
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * are met:
Line 32... Line 33...
32
 
33
 
33
/**
34
/**
34
 * @file
35
 * @file
35
 * @brief   Memory string operations.
36
 * @brief   Memory string operations.
36
 *
37
 *
37
 * This file provides architecture independent functions
38
 * This file provides architecture independent functions to manipulate blocks of
38
 * to manipulate blocks of memory. These functions
-
 
39
 * are optimized as much as generic functions of
39
 * memory. These functions are optimized as much as generic functions of this
40
 * this type can be. However, architectures are
40
 * type can be. However, architectures are free to provide even more optimized
41
 * free to provide even more optimized versions of these
-
 
42
 * functions.
41
 * versions of these functions.
43
 */
42
 */
44
 
43
 
45
#include <memstr.h>
44
#include <memstr.h>
46
#include <arch/types.h>
45
#include <arch/types.h>
47
#include <align.h>
46
#include <align.h>
48
 
47
 
49
/** Copy block of memory
48
/** Copy block of memory.
50
 *
49
 *
51
 * Copy cnt bytes from src address to dst address.
50
 * Copy cnt bytes from src address to dst address.  The copying is done
52
 * The copying is done word-by-word and then byte-by-byte.
51
 * word-by-word and then byte-by-byte.  The source and destination memory areas
53
 * The source and destination memory areas cannot overlap.
52
 * cannot overlap.
54
 *
53
 *
55
 * @param src Origin address to copy from.
54
 * @param src       Source address to copy from.
56
 * @param dst Origin address to copy to.
55
 * @param dst       Destination address to copy to.
57
 * @param cnt Number of bytes to copy.
56
 * @param cnt       Number of bytes to copy.
58
 *
57
 *
-
 
58
 * @return      Destination address.
59
 */
59
 */
60
void *_memcpy(void * dst, const void *src, size_t cnt)
60
void *_memcpy(void *dst, const void *src, size_t cnt)
61
{
61
{
62
    unsigned int i, j;
62
    unsigned int i, j;
63
   
63
   
64
    if (ALIGN_UP((uintptr_t) src, sizeof(unative_t)) != (uintptr_t) src ||
64
    if (ALIGN_UP((uintptr_t) src, sizeof(unative_t)) != (uintptr_t) src ||
65
        ALIGN_UP((uintptr_t) dst, sizeof(unative_t)) != (uintptr_t) dst) {
65
        ALIGN_UP((uintptr_t) dst, sizeof(unative_t)) != (uintptr_t) dst) {
66
        for (i = 0; i < cnt; i++)
66
        for (i = 0; i < cnt; i++)
67
            ((uint8_t *) dst)[i] = ((uint8_t *) src)[i];
67
            ((uint8_t *) dst)[i] = ((uint8_t *) src)[i];
68
    } else {
68
    } else {
69
   
-
 
70
        for (i = 0; i < cnt / sizeof(unative_t); i++)
69
        for (i = 0; i < cnt / sizeof(unative_t); i++)
71
            ((unative_t *) dst)[i] = ((unative_t *) src)[i];
70
            ((unative_t *) dst)[i] = ((unative_t *) src)[i];
72
       
71
       
73
        for (j = 0; j < cnt % sizeof(unative_t); j++)
72
        for (j = 0; j < cnt % sizeof(unative_t); j++)
-
 
73
            ((uint8_t *)(((unative_t *) dst) + i))[j] =
74
            ((uint8_t *)(((unative_t *) dst) + i))[j] = ((uint8_t *)(((unative_t *) src) + i))[j];
74
                ((uint8_t *)(((unative_t *) src) + i))[j];
75
    }
75
    }
76
       
76
       
77
    return (char *) dst;
77
    return (char *) dst;
78
}
78
}
79
 
79
 
-
 
80
/** Move memory block with possible overlapping.
-
 
81
 *
-
 
82
 * Copy cnt bytes from src address to dst address. The source and destination
-
 
83
 * memory areas may overlap.
-
 
84
 *
-
 
85
 * @param src       Source address to copy from.
-
 
86
 * @param dst       Destination address to copy to.
-
 
87
 * @param cnt       Number of bytes to copy.
-
 
88
 *
-
 
89
 * @return      Destination address.
-
 
90
 */
-
 
91
void *memmove(void *dst, const void *src, size_t n)
-
 
92
{
-
 
93
    const uint8_t *sp;
-
 
94
    uint8_t *dp;
-
 
95
 
-
 
96
    /* Nothing to do? */
-
 
97
    if (src == dst)
-
 
98
        return dst;
-
 
99
 
-
 
100
    /* Non-overlapping? */
-
 
101
    if (dst >= src + n || src >= dst + n) {
-
 
102
        return memcpy(dst, src, n);
-
 
103
    }
-
 
104
 
-
 
105
    /* Which direction? */
-
 
106
    if (src > dst) {
-
 
107
        /* Forwards. */
-
 
108
        sp = src;
-
 
109
        dp = dst;
-
 
110
 
-
 
111
        while (n-- != 0)
-
 
112
            *dp++ = *sp++;
-
 
113
    } else {
-
 
114
        /* Backwards. */
-
 
115
        sp = src + (n - 1);
-
 
116
        dp = dst + (n - 1);
-
 
117
 
-
 
118
        while (n-- != 0)
-
 
119
            *dp-- = *sp--;
-
 
120
    }
-
 
121
 
-
 
122
    return dst;
-
 
123
}
-
 
124
 
80
/** Fill block of memory
125
/** Fill block of memory
81
 *
126
 *
82
 * Fill cnt bytes at dst address with the value x.
127
 * Fill cnt bytes at dst address with the value x.  The filling is done
83
 * The filling is done byte-by-byte.
128
 * byte-by-byte.
84
 *
129
 *
85
 * @param dst Origin address to fill.
130
 * @param dst       Destination address to fill.
86
 * @param cnt Number of bytes to fill.
131
 * @param cnt       Number of bytes to fill.
87
 * @param x   Value to fill.
132
 * @param x     Value to fill.
88
 *
133
 *
89
 */
134
 */
90
void _memsetb(void *dst, size_t cnt, uint8_t x)
135
void _memsetb(void *dst, size_t cnt, uint8_t x)
91
{
136
{
92
    unsigned int i;
137
    unsigned int i;
Line 94... Line 139...
94
   
139
   
95
    for (i = 0; i < cnt; i++)
140
    for (i = 0; i < cnt; i++)
96
        p[i] = x;
141
        p[i] = x;
97
}
142
}
98
 
143
 
99
/** Fill block of memory
144
/** Fill block of memory.
100
 *
145
 *
101
 * Fill cnt words at dst address with the value x.
146
 * Fill cnt words at dst address with the value x.  The filling is done
102
 * The filling is done word-by-word.
147
 * word-by-word.
103
 *
148
 *
104
 * @param dst Origin address to fill.
149
 * @param dst       Destination address to fill.
105
 * @param cnt Number of words to fill.
150
 * @param cnt       Number of words to fill.
106
 * @param x   Value to fill.
151
 * @param x     Value to fill.
107
 *
152
 *
108
 */
153
 */
109
void _memsetw(void *dst, size_t cnt, uint16_t x)
154
void _memsetw(void *dst, size_t cnt, uint16_t x)
110
{
155
{
111
    unsigned int i;
156
    unsigned int i;
Line 113... Line 158...
113
   
158
   
114
    for (i = 0; i < cnt; i++)
159
    for (i = 0; i < cnt; i++)
115
        p[i] = x;  
160
        p[i] = x;  
116
}
161
}
117
 
162
 
118
/** Copy string
-
 
119
 *
-
 
120
 * Copy string from src address to dst address.
-
 
121
 * The copying is done char-by-char until the null
-
 
122
 * character. The source and destination memory areas
-
 
123
 * cannot overlap.
-
 
124
 *
-
 
125
 * @param src Origin string to copy from.
-
 
126
 * @param dst Origin string to copy to.
-
 
127
 *
-
 
128
 */
-
 
129
char *strcpy(char *dest, const char *src)
-
 
130
{
-
 
131
    char *orig = dest;
-
 
132
   
-
 
133
    while ((*(dest++) = *(src++)))
-
 
134
        ;
-
 
135
    return orig;
-
 
136
}
-
 
137
 
-
 
138
/** @}
163
/** @}
139
 */
164
 */