Skip to content

Commit 6537334

Browse files
authored
Remove __declspec(noalias) from example
Remove __declspec(noalias) from example to make it clearer what is going on. I'm submitting a second PR that updates __declspec(alias) to have its own example. Clean up other text.
1 parent a5916b4 commit 6537334

File tree

1 file changed

+81
-6
lines changed

1 file changed

+81
-6
lines changed

docs/cpp/restrict.md

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,94 @@ __declspec(restrict) return_type f();
2929
```
3030

3131
## Remarks
32-
The compiler will propagate `__declspec(restrict)`. For example, the CRT `malloc` function is decorated with `__declspec(restrict)` and therefore, pointers initialized to memory locations with `malloc` are also implied to not be aliased.
32+
The compiler will propagate `__declspec(restrict)`. For example, the CRT `malloc` function is decorated with `__declspec(restrict)` and therefore, pointers initialized to memory locations with `malloc` are also implied to not be aliased by previously existing memory.
3333

3434
The compiler does not check that the pointer is actually not aliased. It is the developer's responsibility to ensure the program does not alias a pointer marked with the `restrict __declspec` modifier.
3535

36-
For similar semantics on variables, see [__restrict](../cpp/extension-restrict.md).
36+
For similar semantics on variables, see [__restrict](../cpp/extension-restrict.md).
37+
38+
For another annotation that can impact aliasing, see also [__declspec(noalias)](../cpp/noalias.md).
3739

40+
For information about the restrict keyword that is part of C++ AMP, see [restrict (C++ AMP)](../cpp/restrict-cpp-amp.md).
41+
3842
## Example
39-
See [noalias](../cpp/noalias.md) for an example using `restrict`.
43+
44+
The following sample demonstrates using `__declspec(restrict)`.
45+
46+
Decorating functions that return pointers with `__declspec(restrict)` tells the compiler that the memory pointed to by the return value is not aliased. In this example, the pointers `mempool` and `memptr` are global so the compiler has no assurance that the memory is not subject to aliasing. However, they are used in such a way that each invocation of `ma` and `init` return memory that isn't otherwise referenced by the program, so `__decslpec(restrict)` is used to help the optimizer. This is similar to how the CRT headers decorate allocation functions such `malloc` as `restrict` to indicate that they always return memory that cannot be aliased by existing pointers.
47+
48+
Decorating functions with `__declspec(noalias)` tells the compiler that the function does not interfere with the global state except through the pointers in its parameter list. in the example that accesses memory
49+
50+
```C
51+
// declspec_noalias.c
52+
#include <stdio.h>
53+
#include <stdlib.h>
54+
55+
#define M 800
56+
#define N 600
57+
#define P 700
58+
59+
float * mempool, * memptr;
60+
61+
__declspec(restrict) float * ma(int size)
62+
{
63+
float * retval;
64+
retval = memptr;
65+
memptr += size;
66+
return retval;
67+
}
68+
69+
__declspec(restrict) float * init(int m, int n)
70+
{
71+
float * a;
72+
int i, j;
73+
int k=1;
74+
75+
a = ma(m * n);
76+
if (!a) exit(1);
77+
for (i=0; i<m; i++)
78+
for (j=0; j<n; j++)
79+
a[i*n+j] = 0.1/k++;
80+
return a;
81+
}
82+
83+
void multiply(float * a, float * b, float * c)
84+
{
85+
int i, j, k;
86+
87+
for (j=0; j<P; j++)
88+
for (i=0; i<M; i++)
89+
for (k=0; k<N; k++)
90+
c[i * P + j] =
91+
a[i * N + k] *
92+
b[k * P + j];
93+
}
94+
95+
int main()
96+
{
97+
float * a, * b, * c;
98+
99+
mempool = (float *) malloc(sizeof(float) * (M*N + N*P + M*P));
100+
101+
if (!mempool)
102+
{
103+
puts("ERROR: Malloc returned null");
104+
exit(1);
105+
}
106+
107+
memptr = mempool;
108+
a = init(M, N);
109+
b = init(N, P);
110+
c = init(M, P);
111+
112+
multiply(a, b, c);
113+
}
114+
```
40115
41-
For information about the restrict keyword that is part of C++ AMP, see [restrict (C++ AMP)](../cpp/restrict-cpp-amp.md).
42116
43117
**END Microsoft Specific**
44118
45119
## See Also
46-
[__declspec](../cpp/declspec.md)
47-
[Keywords](../cpp/keywords-cpp.md)
120+
[__declspec](../cpp/declspec.md)
121+
[Keywords](../cpp/keywords-cpp.md)
122+
[__declspec(noalias)](../cpp/noalias.md)

0 commit comments

Comments
 (0)