Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Memory-allocation/00-META.yaml
Normal file
3
Task/Memory-allocation/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Memory_allocation
|
||||
note: Basic language learning
|
||||
6
Task/Memory-allocation/00-TASK.txt
Normal file
6
Task/Memory-allocation/00-TASK.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
;Task:
|
||||
Show how to explicitly allocate and deallocate blocks of memory in your language.
|
||||
|
||||
Show access to different types of memory (i.e., [[heap]], [[system stack|stack]], shared, foreign) if applicable.
|
||||
<br><br>
|
||||
|
||||
22
Task/Memory-allocation/360-Assembly/memory-allocation-1.360
Normal file
22
Task/Memory-allocation/360-Assembly/memory-allocation-1.360
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
* Request to Get Storage Managed by "GETMAIN" Supervisor Call (SVC 4)
|
||||
LA 1,PLIST Point Reg 1 to GETMAIN/FREEMAIN Parm List
|
||||
SVC 4 Issue GETMAIN SVC
|
||||
LTR 15,15 Register 15 = 0?
|
||||
BZ GOTSTG Yes: Got Storage
|
||||
* [...] No: Handle GETMAIN Failure
|
||||
GOTSTG L 2,STG@ Load Reg (any Reg) with Addr of Aquired Stg
|
||||
* [...] Continue
|
||||
* Request to Free Storage Managed by "FREEMAIN" Supervisor Call (SVC 5)
|
||||
LA 1,PLIST Point Reg 1 to GETMAIN/FREEMAIN Parm List
|
||||
SVC 5 Issue FREEMAIN SVC
|
||||
LTR 15,15 Register 15 = 0?
|
||||
BZ STGFRE Yes: Storage Freed
|
||||
* [...] No: Handle FREEMAIN Failure
|
||||
STGFRE EQU * Storage Freed
|
||||
* [...] Continue
|
||||
*
|
||||
STG@ DS A Address of Stg Area (Aquired or to be Freed)
|
||||
PLIST EQU * 10-Byte GETMAIN/FREEMAIN Parameter List
|
||||
DC A(256) Number of Bytes; Max=16777208 ((2**24)-8)
|
||||
DC A(STG@) Pointer to Address of Storage Area
|
||||
DC X'0000' (Unconditional Request; Subpool 0)
|
||||
33
Task/Memory-allocation/360-Assembly/memory-allocation-2.360
Normal file
33
Task/Memory-allocation/360-Assembly/memory-allocation-2.360
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
STOREXNO AMODE 31
|
||||
STOREXNO RMODE ANY
|
||||
STOREXNO CSECT ,
|
||||
SYSSTATE AMODE64=NO,ARCHLVL=3 gen z9+, z/OS 2.1+ bin code
|
||||
IEABRCX DEFINE convert based to relative branches
|
||||
BAKR 14,0 callers registers to linkage stack
|
||||
LARL 12,CONSTANTS load address relative long
|
||||
USING CONSTANTS,12 using for constants
|
||||
LA 9,WALEN load memory length in Register 9
|
||||
STORAGE OBTAIN,LENGTH=(9),EXECUTABLE=NO,LOC=ANY
|
||||
LR 10,1 Reg1 holds address of mem area
|
||||
USING DYNAREA,10 using for dynamic memory area
|
||||
LA 13,SAVEA PC routine convention: ...
|
||||
MVC SAVEA+4(4),=C'F1SA' ... format 1 savearea: L-stack
|
||||
*
|
||||
* copy instruction sequence SR Reg15,Reg15; Branch Reg14 to DATA1
|
||||
* in obtained storage location, and branch to it
|
||||
*
|
||||
MVC DATA1(8),=X'1BFF07FE00000000' SR 15,15; BR 14
|
||||
LA 7,DATA1
|
||||
BASR 14,7 This will OC4-4 with EXECUTABLE=NO
|
||||
STORAGE RELEASE,ADDR=(10),LENGTH=(9),EXECUTABLE=NO
|
||||
PR , return to caller
|
||||
CONSTANTS DS 0D constant section, aligned for LARL
|
||||
DC C'SOMEDATA'
|
||||
DC C'SOMEOTHERDATA'
|
||||
LTORG , have assembler build literal pool
|
||||
DYNAREA DSECT
|
||||
SAVEA DS 18F
|
||||
DATA1 DS 2F
|
||||
DATA2 DS CL256 can receive any value
|
||||
WALEN EQU *-DYNAREA length of obtained area
|
||||
END STOREXNO end of module
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
LDA #$FF ;load 255 into the accumulator
|
||||
STA $00 ;store at zero page memory address $00
|
||||
STA $0400 ;store at absolute memory address $0400
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
MyFunction:
|
||||
LINK A6,#-16 ;create a stack frame of 16 bytes. Now you can safely write to (SP+0) thru (SP+15).
|
||||
|
||||
;;;; your code goes here.
|
||||
|
||||
UNLK A6 ;free the stack frame
|
||||
RTS
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
MOVE.L An,-(SP)
|
||||
MOVEA.L SP,An
|
||||
LEA (-disp,SP),SP
|
||||
1
Task/Memory-allocation/ALGOL-68/memory-allocation-1.alg
Normal file
1
Task/Memory-allocation/ALGOL-68/memory-allocation-1.alg
Normal file
|
|
@ -0,0 +1 @@
|
|||
MODE MYSTRUCT = STRUCT(INT i, j, k, REAL r, COMPL c);
|
||||
1
Task/Memory-allocation/ALGOL-68/memory-allocation-2.alg
Normal file
1
Task/Memory-allocation/ALGOL-68/memory-allocation-2.alg
Normal file
|
|
@ -0,0 +1 @@
|
|||
REF MYSTRUCT l = LOC MYSTRUCT;
|
||||
1
Task/Memory-allocation/ALGOL-68/memory-allocation-3.alg
Normal file
1
Task/Memory-allocation/ALGOL-68/memory-allocation-3.alg
Normal file
|
|
@ -0,0 +1 @@
|
|||
REF MYSTRUCT h = HEAP MYSTRUCT;
|
||||
3
Task/Memory-allocation/ALGOL-68/memory-allocation-4.alg
Normal file
3
Task/Memory-allocation/ALGOL-68/memory-allocation-4.alg
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[666]MYSTRUCT pool;
|
||||
INT new pool := LWB pool-1;
|
||||
REF MYSTRUCT p = pool[new pool +:=1];
|
||||
1
Task/Memory-allocation/ALGOL-68/memory-allocation-5.alg
Normal file
1
Task/Memory-allocation/ALGOL-68/memory-allocation-5.alg
Normal file
|
|
@ -0,0 +1 @@
|
|||
MYSTRUCT i;
|
||||
10
Task/Memory-allocation/ALGOL-W/memory-allocation.alg
Normal file
10
Task/Memory-allocation/ALGOL-W/memory-allocation.alg
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
begin
|
||||
% define a record structure - instances must be created dynamically %
|
||||
record Element ( integer atomicNumber; string(16) name );
|
||||
reference(Element) X;
|
||||
% allocate and initialise memory for X - heap storage is the only option %
|
||||
X := Element( 1, "Hydrogen" );
|
||||
% allocate new memory for X, the original could now be garbage collected %
|
||||
X := Element( 2, "Helium" )
|
||||
% the memory allocated will now be garbage collected - there is no explicit de-allocation %
|
||||
end.
|
||||
14
Task/Memory-allocation/Action-/memory-allocation.action
Normal file
14
Task/Memory-allocation/Action-/memory-allocation.action
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
CARD EndProg ;required for ALLOCATE.ACT
|
||||
|
||||
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
|
||||
|
||||
PROC Main()
|
||||
DEFINE SIZE="1000"
|
||||
BYTE POINTER ptr
|
||||
|
||||
AllocInit(EndProg) ;required before any memory allocation
|
||||
|
||||
ptr=Alloc(SIZE) ;allocate memory of 1000 bytes
|
||||
SetBlock(ptr,SIZE,$FF) ;fill the memory block with $FF
|
||||
Free(ptr,SIZE) ;free allocated memory
|
||||
RETURN
|
||||
5
Task/Memory-allocation/Ada/memory-allocation-1.ada
Normal file
5
Task/Memory-allocation/Ada/memory-allocation-1.ada
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
declare
|
||||
X : Integer; -- Allocated on the stack
|
||||
begin
|
||||
...
|
||||
end; -- X is freed
|
||||
6
Task/Memory-allocation/Ada/memory-allocation-2.ada
Normal file
6
Task/Memory-allocation/Ada/memory-allocation-2.ada
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
declare
|
||||
type Integer_Ptr is access Integer;
|
||||
Ptr : Integer_Ptr := new Integer; -- Allocated in the heap
|
||||
begin
|
||||
...
|
||||
end; -- Memory is freed because Integer_Ptr is finalized
|
||||
8
Task/Memory-allocation/Ada/memory-allocation-3.ada
Normal file
8
Task/Memory-allocation/Ada/memory-allocation-3.ada
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
declare
|
||||
type Integer_Ptr is access Integer;
|
||||
procedure Free is new Ada.Unchecked_Deallocation (Integer, Integer_Ptr)
|
||||
Ptr : Integer_Ptr := new Integer; -- Allocated in the heap
|
||||
begin
|
||||
Free (Ptr); -- Explicit deallocation
|
||||
...
|
||||
end;
|
||||
3
Task/Memory-allocation/Ada/memory-allocation-4.ada
Normal file
3
Task/Memory-allocation/Ada/memory-allocation-4.ada
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
package P is
|
||||
X : Integer; -- Allocated in the result the package elaboration
|
||||
end P;
|
||||
2
Task/Memory-allocation/Arturo/memory-allocation.arturo
Normal file
2
Task/Memory-allocation/Arturo/memory-allocation.arturo
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
myBlock: @[1 2 3]
|
||||
'myBlock ++ [4 5 6]
|
||||
2
Task/Memory-allocation/AutoHotkey/memory-allocation.ahk
Normal file
2
Task/Memory-allocation/AutoHotkey/memory-allocation.ahk
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
VarSetCapacity(Var, 10240000) ; allocate 10 megabytes
|
||||
VarSetCapacity(Var, 0) ; free it
|
||||
2
Task/Memory-allocation/Axe/memory-allocation.axe
Normal file
2
Task/Memory-allocation/Axe/memory-allocation.axe
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Buff(100)→Str1
|
||||
.Str1 points to a 100-byte memory region allocated at compile time
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
size% = 12345
|
||||
DIM mem% size%-1
|
||||
PRINT ; size% " bytes of heap allocated at " ; mem%
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
size% = 12345
|
||||
PROCstack(size%)
|
||||
END
|
||||
|
||||
DEF PROCstack(s%)
|
||||
LOCAL mem%
|
||||
DIM mem% LOCAL s%-1
|
||||
PRINT ; s% " bytes of stack allocated at " ; mem%
|
||||
ENDPROC
|
||||
10
Task/Memory-allocation/Bracmat/memory-allocation.bracmat
Normal file
10
Task/Memory-allocation/Bracmat/memory-allocation.bracmat
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
( alc$2000:?p {allocate 2000 bytes}
|
||||
& pok$(!p,123456789,4) { poke a large value as a 4 byte integer }
|
||||
& pok$(!p+4,0,4) { poke zeros in the next 4 bytes }
|
||||
& out$(pee$(!p,1)) { peek the first byte }
|
||||
& out$(pee$(!p+2,2)) { peek the short int located at the third and fourth byte }
|
||||
& out$(pee$(!p,4)) { peek the first four bytes }
|
||||
& out$(pee$(!p+6,2)) { peek the two bytes from the zeroed-out range }
|
||||
& out$(pee$(!p+1000,2)) { peek some uninitialized data }
|
||||
& fre$!p { free the memory }
|
||||
&);
|
||||
23
Task/Memory-allocation/C++/memory-allocation-1.cpp
Normal file
23
Task/Memory-allocation/C++/memory-allocation-1.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include <string>
|
||||
|
||||
int main()
|
||||
{
|
||||
int* p;
|
||||
|
||||
p = new int; // allocate a single int, uninitialized
|
||||
delete p; // deallocate it
|
||||
|
||||
p = new int(2); // allocate a single int, initialized with 2
|
||||
delete p; // deallocate it
|
||||
|
||||
std::string* p2;
|
||||
|
||||
p2 = new std::string; // allocate a single string, default-initialized
|
||||
delete p2; // deallocate it
|
||||
|
||||
p = new int[10]; // allocate an array of 10 ints, uninitialized
|
||||
delete[] p; // deallocation of arrays must use delete[]
|
||||
|
||||
p2 = new std::string[10]; // allocate an array of 10 strings, default-initialized
|
||||
delete[] p2; // deallocate it
|
||||
}
|
||||
5
Task/Memory-allocation/C++/memory-allocation-2.cpp
Normal file
5
Task/Memory-allocation/C++/memory-allocation-2.cpp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
int main()
|
||||
{
|
||||
void* memory = operator new(20); // allocate 20 bytes of memory
|
||||
operator delete(memory); // deallocate it
|
||||
}
|
||||
12
Task/Memory-allocation/C++/memory-allocation-3.cpp
Normal file
12
Task/Memory-allocation/C++/memory-allocation-3.cpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#include <new>
|
||||
|
||||
int main()
|
||||
{
|
||||
union
|
||||
{
|
||||
int alignment_dummy; // make sure the block is correctly aligned for ints
|
||||
char data[2*sizeof(int)]; // enough space for 10 ints
|
||||
};
|
||||
int* p = new(&data) int(3); // construct an int at the beginning of data
|
||||
new(p+1) int(5); // construct another int directly following
|
||||
}
|
||||
2
Task/Memory-allocation/C++/memory-allocation-4.cpp
Normal file
2
Task/Memory-allocation/C++/memory-allocation-4.cpp
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
void* memory_for_p = operator new(sizeof(int));
|
||||
int* p = new(memory_for_p) int(3);
|
||||
3
Task/Memory-allocation/C++/memory-allocation-5.cpp
Normal file
3
Task/Memory-allocation/C++/memory-allocation-5.cpp
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#include <new>
|
||||
|
||||
int* p = new(std::nothrow) int(3);
|
||||
27
Task/Memory-allocation/C++/memory-allocation-6.cpp
Normal file
27
Task/Memory-allocation/C++/memory-allocation-6.cpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <new>
|
||||
|
||||
class MyClass
|
||||
{
|
||||
public:
|
||||
void* operator new(std::size_t size)
|
||||
{
|
||||
void* p = std::malloc(size);
|
||||
if (!p) throw std::bad_alloc();
|
||||
return p;
|
||||
}
|
||||
void operator delete(void* p)
|
||||
{
|
||||
free(p);
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
MyClass* p = new MyClass; // uses class specific operator new
|
||||
delete p; // uses class specific operator delete
|
||||
|
||||
int* p2 = new int; // uses default operator new
|
||||
delete p2; // uses default operator delete
|
||||
}
|
||||
15
Task/Memory-allocation/C++/memory-allocation-7.cpp
Normal file
15
Task/Memory-allocation/C++/memory-allocation-7.cpp
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
class arena { /* ... */ };
|
||||
|
||||
void* operator new(std::size_t size, arena& a)
|
||||
{
|
||||
return arena.alloc(size);
|
||||
}
|
||||
|
||||
void operator delete(void* p, arena& a)
|
||||
{
|
||||
arena.dealloc(p);
|
||||
}
|
||||
|
||||
arena whatever(/* ... */);
|
||||
|
||||
int* p = new(whatever) int(3); // uses operator new from above to allocate from the arena whatever
|
||||
9
Task/Memory-allocation/C++/memory-allocation-8.cpp
Normal file
9
Task/Memory-allocation/C++/memory-allocation-8.cpp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
class MyClass { /*...*/ };
|
||||
|
||||
int main()
|
||||
{
|
||||
MyClass* p = new(whatever) MyClass; // allocate memory for myclass from arena and construct a MyClass object there
|
||||
// ...
|
||||
p->~MyClass(); // explicitly destruct *p
|
||||
operator delete(p, whatever); // explicitly deallocate the memory
|
||||
}
|
||||
37
Task/Memory-allocation/C-sharp/memory-allocation.cs
Normal file
37
Task/Memory-allocation/C-sharp/memory-allocation.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
public unsafe class Program
|
||||
{
|
||||
public static unsafe void HeapMemory()
|
||||
{
|
||||
const int HEAP_ZERO_MEMORY = 0x00000008;
|
||||
const int size = 1000;
|
||||
int ph = GetProcessHeap();
|
||||
void* pointer = HeapAlloc(ph, HEAP_ZERO_MEMORY, size);
|
||||
if (pointer == null)
|
||||
throw new OutOfMemoryException();
|
||||
Console.WriteLine(HeapSize(ph, 0, pointer));
|
||||
HeapFree(ph, 0, pointer);
|
||||
}
|
||||
|
||||
public static unsafe void StackMemory()
|
||||
{
|
||||
byte* buffer = stackalloc byte[1000];
|
||||
// buffer is automatically discarded when the method returns
|
||||
}
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
HeapMemory();
|
||||
StackMemory();
|
||||
}
|
||||
[DllImport("kernel32")]
|
||||
static extern void* HeapAlloc(int hHeap, int flags, int size);
|
||||
[DllImport("kernel32")]
|
||||
static extern bool HeapFree(int hHeap, int flags, void* block);
|
||||
[DllImport("kernel32")]
|
||||
static extern int GetProcessHeap();
|
||||
[DllImport("kernel32")]
|
||||
static extern int HeapSize(int hHeap, int flags, void* block);
|
||||
|
||||
}
|
||||
18
Task/Memory-allocation/C/memory-allocation-1.c
Normal file
18
Task/Memory-allocation/C/memory-allocation-1.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
/* size of "members", in bytes */
|
||||
#define SIZEOF_MEMB (sizeof(int))
|
||||
#define NMEMB 100
|
||||
|
||||
int main()
|
||||
{
|
||||
int *ints = malloc(SIZEOF_MEMB*NMEMB);
|
||||
/* realloc can be used to increase or decrease an already
|
||||
allocated memory (same as malloc if ints is NULL) */
|
||||
ints = realloc(ints, sizeof(int)*(NMEMB+1));
|
||||
/* calloc set the memory to 0s */
|
||||
int *int2 = calloc(NMEMB, SIZEOF_MEMB);
|
||||
/* all use the same free */
|
||||
free(ints); free(int2);
|
||||
return 0;
|
||||
}
|
||||
17
Task/Memory-allocation/C/memory-allocation-2.c
Normal file
17
Task/Memory-allocation/C/memory-allocation-2.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
int func()
|
||||
{
|
||||
int ints[NMEMB]; /* it resembles malloc ... */
|
||||
int *int2; /* here the only thing allocated on the stack is a pointer */
|
||||
char intstack[SIZEOF_MEMB*NMEMB]; /* to show resemblance to malloc */
|
||||
int2 = (int *)intstack; /* but this is educative, do not do so unless... */
|
||||
|
||||
{
|
||||
const char *pointers_to_char[NMEMB];
|
||||
/* use pointers_to_char */
|
||||
pointers_to_char[0] = "educative";
|
||||
} /* outside the block, the variable "disappears" */
|
||||
|
||||
/* here we can use ints, int2, intstack vars, which are not seen elsewhere of course */
|
||||
|
||||
return 0;
|
||||
}
|
||||
8
Task/Memory-allocation/C/memory-allocation-3.c
Normal file
8
Task/Memory-allocation/C/memory-allocation-3.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <alloca.h>
|
||||
int *funcA()
|
||||
{
|
||||
int *ints = alloca(SIZEOF_MEMB*NMEMB);
|
||||
ints[0] = 0; /* use it */
|
||||
return ints; /* BUT THIS IS WRONG! It is not like malloc: the memory
|
||||
does not "survive"! */
|
||||
}
|
||||
14
Task/Memory-allocation/C/memory-allocation-4.c
Normal file
14
Task/Memory-allocation/C/memory-allocation-4.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/* this is global */
|
||||
int integers[NMEMB]; /* should be initialized with 0s */
|
||||
|
||||
int funcB()
|
||||
{
|
||||
static int ints[NMEMB]; /* this is "static", i.e. the memory "survive" even
|
||||
when the function exits, but the symbol's scope is local */
|
||||
return integers[0] + ints[0];
|
||||
}
|
||||
|
||||
void funcC(int a)
|
||||
{
|
||||
integers[0] = a;
|
||||
}
|
||||
15
Task/Memory-allocation/COBOL/memory-allocation.cobol
Normal file
15
Task/Memory-allocation/COBOL/memory-allocation.cobol
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
PROGRAM-ID. memory-allocation.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 based-data PIC X(20) VALUE "Hello, World!"
|
||||
BASED.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
*> INITIALIZED sets the data item to the VALUE.
|
||||
ALLOCATE based-data INITIALIZED
|
||||
DISPLAY based-data
|
||||
FREE based-data
|
||||
|
||||
GOBACK
|
||||
.
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(defun show-allocation ()
|
||||
(let ((a (cons 1 2))
|
||||
(b (cons 1 2)))
|
||||
(declare (dynamic-extent b))
|
||||
(list a b)))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(show-allocation)
|
||||
135
Task/Memory-allocation/D/memory-allocation.d
Normal file
135
Task/Memory-allocation/D/memory-allocation.d
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
// D is a system language so its memory management is refined.
|
||||
// D supports thread-local memory on default, global memory, memory
|
||||
// allocated on the stack, the C heap, or the D heap managed by a
|
||||
// garbage collector, both manually and automatically.
|
||||
|
||||
// This program looks scary because its purpose is to show all the
|
||||
// variety. But lot of this stuff is only for special situations
|
||||
// (like alloca), and it's not necessary in most user code.
|
||||
|
||||
enum int nInts = 10; // Compile-time constant.
|
||||
|
||||
// This is thread-local:
|
||||
int[nInts] data1;
|
||||
|
||||
// This is global:
|
||||
__gshared int[nInts] data2;
|
||||
|
||||
void main() {
|
||||
// Static memory, it's thread-local but its name is usable
|
||||
// only locally:
|
||||
static int[nInts] data3;
|
||||
|
||||
// Static memory, it's global but its name is usable only locally:
|
||||
__gshared static int[nInts] data4;
|
||||
|
||||
// ----------------------
|
||||
// D supports the functions that manage memory of the C heap:
|
||||
import core.stdc.stdlib: malloc, calloc, realloc, free, alloca;
|
||||
|
||||
// Allocates space for some integers on the heap,
|
||||
// the memory is not initialized:
|
||||
auto ptr1 = cast(int*)malloc(nInts * int.sizeof);
|
||||
if (ptr1 == null)
|
||||
return;
|
||||
|
||||
// Increases the space for one more integer, the new space
|
||||
// is not initialized, but the old space is not modified:
|
||||
ptr1 = cast(int*)realloc(ptr1, (nInts + 1) * int.sizeof);
|
||||
if (ptr1 == null)
|
||||
return;
|
||||
|
||||
// calloc allocates on the heap and zeros the memory:
|
||||
auto ptr2 = cast(int*)calloc(nInts, int.sizeof);
|
||||
if (ptr2 == null)
|
||||
return;
|
||||
|
||||
// You can create a slice from a pointer:
|
||||
auto slice1 = ptr2[0 .. nInts];
|
||||
|
||||
// Frees the memory:
|
||||
free(ptr2);
|
||||
free(ptr1);
|
||||
|
||||
// ----------------------
|
||||
import core.stdc.stdio: puts;
|
||||
|
||||
static struct Test {
|
||||
~this() { puts("Test destructor"); }
|
||||
}
|
||||
|
||||
// Memory allocated on the stack:
|
||||
Test[2] array1;
|
||||
|
||||
{
|
||||
// More memory allocated on the stack:
|
||||
Test[2] array2;
|
||||
// Here array2 is removed from the stack,
|
||||
// and all array2 destructors get called.
|
||||
}
|
||||
puts("Block end.");
|
||||
|
||||
// alloca is supported in D. It's similar to malloc but the
|
||||
// memory is allocated on the stack:
|
||||
int* ptr3 = cast(int*)alloca(nInts * int.sizeof);
|
||||
|
||||
// You can create a slice from the pointer:
|
||||
auto slice2 = ptr3[0 .. nInts];
|
||||
|
||||
// Do not free the memory allocated with alloca:
|
||||
// free(ptr3);
|
||||
|
||||
// ----------------------
|
||||
// Allocates a dynamic array on the D heap managed by
|
||||
// the D garbage collector:
|
||||
auto array3 = new int[nInts];
|
||||
|
||||
// Try to reserve capacity for a dynamic array on the D heap:
|
||||
int[] array4;
|
||||
array4.reserve(nInts);
|
||||
assert(array4.capacity >= nInts);
|
||||
assert(array4.length == 0);
|
||||
|
||||
// Appends one integer to the dynamic array:
|
||||
array4 ~= 100;
|
||||
|
||||
// Assume that it is safe to append to this array. Appends made
|
||||
// to this array after calling this function may append in place,
|
||||
// even if the array was a slice of a larger array to begin with:
|
||||
array4.assumeSafeAppend;
|
||||
array4 ~= 200;
|
||||
array4 ~= 300;
|
||||
assert(array4.length == 3);
|
||||
// See here for more info:
|
||||
// http://dlang.org/d-array-article.html
|
||||
|
||||
|
||||
// Allocates a struct and a class on the D GC heap:
|
||||
static class Foo { int x; }
|
||||
Test* t = new Test; // This destructor will not be called.
|
||||
Foo f1 = new Foo; // f1 is a class reference.
|
||||
|
||||
// Optional. Destroys the given object and puts it in
|
||||
// an invalid state:
|
||||
f1.destroy;
|
||||
|
||||
import std.typecons: scoped;
|
||||
|
||||
// Allocates a class on the stack, unsafe:
|
||||
auto f3 = scoped!Foo();
|
||||
|
||||
// ----------------------
|
||||
import core.memory: GC;
|
||||
|
||||
// Allocates an aligned block from the GC, initialized to zero.
|
||||
// Plus it doesn't scan through this block on collect.
|
||||
auto ptr4 = cast(int*)GC.calloc(nInts * int.sizeof,
|
||||
GC.BlkAttr.NO_SCAN);
|
||||
|
||||
// No need to test for this, because GC.calloc usually
|
||||
// throws OutOfMemoryError if it can't allocate.
|
||||
// if (ptr4 == null)
|
||||
// exit(1);
|
||||
|
||||
GC.free(ptr4); // This is optional.
|
||||
}
|
||||
2
Task/Memory-allocation/E/memory-allocation.e
Normal file
2
Task/Memory-allocation/E/memory-allocation.e
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
? <elib:tables.makeFlexList>.fromType(<type:java.lang.Byte>, 128)
|
||||
# value: [].diverge()
|
||||
1
Task/Memory-allocation/Factor/memory-allocation-1.factor
Normal file
1
Task/Memory-allocation/Factor/memory-allocation-1.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
2000 malloc (...do stuff..) free
|
||||
6
Task/Memory-allocation/Factor/memory-allocation-2.factor
Normal file
6
Task/Memory-allocation/Factor/memory-allocation-2.factor
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
STRUCT: foo { a int } { b foo* } ;
|
||||
|
||||
[
|
||||
foo malloc-struct &free ! gets freed at end of the current with-destructors scope
|
||||
! do stuff
|
||||
] with-destructors
|
||||
10
Task/Memory-allocation/Forth/memory-allocation-1.fth
Normal file
10
Task/Memory-allocation/Forth/memory-allocation-1.fth
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
unused . \ memory available for use in dictionary
|
||||
here . \ current dictionary memory pointer
|
||||
: mem, ( addr len -- ) here over allot swap move ;
|
||||
: s, ( str len -- ) here over char+ allot place align ; \ built-in on some forths
|
||||
: ," [char] " parse s, ;
|
||||
variable num
|
||||
create array 60 cells allot
|
||||
create struct 0 , 10 , char A c, ," string"
|
||||
unused .
|
||||
here .
|
||||
5
Task/Memory-allocation/Forth/memory-allocation-2.fth
Normal file
5
Task/Memory-allocation/Forth/memory-allocation-2.fth
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
marker foo
|
||||
: temp ... ;
|
||||
create dummy 300 allot
|
||||
-150 allot \ trim the size of dummy by 150 bytes
|
||||
foo \ removes foo, temp, and dummy from the list of definitions
|
||||
3
Task/Memory-allocation/Forth/memory-allocation-3.fth
Normal file
3
Task/Memory-allocation/Forth/memory-allocation-3.fth
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
4096 allocate throw ( addr )
|
||||
dup 4096 erase
|
||||
( addr ) free throw
|
||||
15
Task/Memory-allocation/Fortran/memory-allocation.f
Normal file
15
Task/Memory-allocation/Fortran/memory-allocation.f
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
program allocation_test
|
||||
implicit none
|
||||
real, dimension(:), allocatable :: vector
|
||||
real, dimension(:, :), allocatable :: matrix
|
||||
real, pointer :: ptr
|
||||
integer, parameter :: n = 100 ! Size to allocate
|
||||
|
||||
allocate(vector(n)) ! Allocate a vector
|
||||
allocate(matrix(n, n)) ! Allocate a matrix
|
||||
allocate(ptr) ! Allocate a pointer
|
||||
|
||||
deallocate(vector) ! Deallocate a vector
|
||||
deallocate(matrix) ! Deallocate a matrix
|
||||
deallocate(ptr) ! Deallocate a pointer
|
||||
end program allocation_test
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Dim As Integer size = 12345
|
||||
Dim As Integer mem = size-1
|
||||
Print size; " bytes of heap allocated at " ; mem
|
||||
Clear (mem, , 10)
|
||||
Print size; " bytes of heap allocated at " ; mem
|
||||
10
Task/Memory-allocation/FreeBASIC/memory-allocation-2.basic
Normal file
10
Task/Memory-allocation/FreeBASIC/memory-allocation-2.basic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
Dim As Integer size = 12345
|
||||
Dim As Integer mem = size-1
|
||||
|
||||
Sub Stack(s As Integer)
|
||||
Dim As Integer mem = s-1
|
||||
Print s; " bytes of stack allocated at " ; mem
|
||||
End Sub
|
||||
|
||||
Stack(size)
|
||||
Print size; " bytes of stack allocated at " ; mem
|
||||
4
Task/Memory-allocation/Go/memory-allocation-1.go
Normal file
4
Task/Memory-allocation/Go/memory-allocation-1.go
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
func inc(n int) {
|
||||
x := n + 1
|
||||
println(x)
|
||||
}
|
||||
4
Task/Memory-allocation/Go/memory-allocation-2.go
Normal file
4
Task/Memory-allocation/Go/memory-allocation-2.go
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
func inc(n int) *int {
|
||||
x := n + 1
|
||||
return &x
|
||||
}
|
||||
1
Task/Memory-allocation/Go/memory-allocation-3.go
Normal file
1
Task/Memory-allocation/Go/memory-allocation-3.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
type s struct{a, b int}
|
||||
1
Task/Memory-allocation/Go/memory-allocation-4.go
Normal file
1
Task/Memory-allocation/Go/memory-allocation-4.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
&s{}
|
||||
1
Task/Memory-allocation/Go/memory-allocation-5.go
Normal file
1
Task/Memory-allocation/Go/memory-allocation-5.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
new(s)
|
||||
3
Task/Memory-allocation/Go/memory-allocation-6.go
Normal file
3
Task/Memory-allocation/Go/memory-allocation-6.go
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
make([]int, 3)
|
||||
make(map[int]int)
|
||||
make(chan int)
|
||||
9
Task/Memory-allocation/Haskell/memory-allocation-1.hs
Normal file
9
Task/Memory-allocation/Haskell/memory-allocation-1.hs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import Foreign
|
||||
|
||||
bytealloc :: IO ()
|
||||
bytealloc = do
|
||||
a0 <- mallocBytes 100 -- Allocate 100 bytes
|
||||
free a0 -- Free them again
|
||||
allocaBytes 100 $ \a -> -- Allocate 100 bytes; automatically
|
||||
-- freed when closure finishes
|
||||
poke (a::Ptr Word32) 0
|
||||
8
Task/Memory-allocation/Haskell/memory-allocation-2.hs
Normal file
8
Task/Memory-allocation/Haskell/memory-allocation-2.hs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import Foreign
|
||||
|
||||
typedalloc :: IO ()
|
||||
typedalloc = do
|
||||
w <- malloc
|
||||
poke w (100 :: Word32)
|
||||
free w
|
||||
alloca $ \a -> poke a (100 :: Word32)
|
||||
3
Task/Memory-allocation/Icon/memory-allocation.icon
Normal file
3
Task/Memory-allocation/Icon/memory-allocation.icon
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
t := table() # The table's memory is allocated
|
||||
#... do things with t
|
||||
t := &null # The table's memory can be reclaimed
|
||||
3
Task/Memory-allocation/J/memory-allocation-1.j
Normal file
3
Task/Memory-allocation/J/memory-allocation-1.j
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
require 'dll'
|
||||
mema 1000
|
||||
57139856
|
||||
1
Task/Memory-allocation/J/memory-allocation-2.j
Normal file
1
Task/Memory-allocation/J/memory-allocation-2.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
memf 57139856
|
||||
5
Task/Memory-allocation/Java/memory-allocation-1.java
Normal file
5
Task/Memory-allocation/Java/memory-allocation-1.java
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
//All of these objects will be deallocated automatically once the program leaves
|
||||
//their scope and there are no more pointers to the objects
|
||||
Object foo = new Object(); //Allocate an Object and a reference to it
|
||||
int[] fooArray = new int[size]; //Allocate all spaces in an array and a reference to it
|
||||
int x = 0; //Allocate an integer and set its value to 0
|
||||
7
Task/Memory-allocation/Java/memory-allocation-2.java
Normal file
7
Task/Memory-allocation/Java/memory-allocation-2.java
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
public class Blah{
|
||||
//...other methods/data members...
|
||||
protected void finalize() throws Throwable{
|
||||
//Finalization code here
|
||||
}
|
||||
//...other methods/data members...
|
||||
}
|
||||
12
Task/Memory-allocation/Java/memory-allocation-3.java
Normal file
12
Task/Memory-allocation/Java/memory-allocation-3.java
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
public class NoFinalize {
|
||||
public static final void main(String[] params) {
|
||||
NoFinalize nf = new NoFinalize();
|
||||
}
|
||||
public NoFinalize() {
|
||||
System.out.println("created");
|
||||
}
|
||||
@Override
|
||||
protected void finalize() {
|
||||
System.out.println("finalized");
|
||||
}
|
||||
}
|
||||
2
Task/Memory-allocation/Julia/memory-allocation.julia
Normal file
2
Task/Memory-allocation/Julia/memory-allocation.julia
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
matrix = Array{Float64,2}(100,100)
|
||||
matrix[31,42] = pi
|
||||
30
Task/Memory-allocation/Kotlin/memory-allocation.kotlin
Normal file
30
Task/Memory-allocation/Kotlin/memory-allocation.kotlin
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// version 1.1.2
|
||||
|
||||
class MyClass(val myInt: Int) {
|
||||
// in theory this method should be called automatically prior to GC
|
||||
protected fun finalize() {
|
||||
println("MyClass being finalized...")
|
||||
}
|
||||
}
|
||||
|
||||
fun myFun() {
|
||||
val mc: MyClass = MyClass(2) // new non-nullable MyClass object allocated on the heap
|
||||
println(mc.myInt)
|
||||
var mc2: MyClass? = MyClass(3) // new nullable MyClass object allocated on the heap
|
||||
println(mc2?.myInt)
|
||||
mc2 = null // allowed as mc2 is nullable
|
||||
println(mc2?.myInt)
|
||||
// 'mc' and 'mc2' both become eligible for garbage collection here as no longer used
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
myFun()
|
||||
Thread.sleep(3000) // allow time for GC to execute
|
||||
val i: Int = 4 // new non-nullable Int allocated on stack
|
||||
println(i)
|
||||
var j: Int? = 5 // new nullable Int allocated on heap
|
||||
println(j)
|
||||
j = null // allowed as 'j' is nullable
|
||||
println(j)
|
||||
// 'j' becomes eligible for garbage collection here as no longer used
|
||||
}
|
||||
6
Task/Memory-allocation/Lingo/memory-allocation.lingo
Normal file
6
Task/Memory-allocation/Lingo/memory-allocation.lingo
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
-- Create a ByteArray of 100 Kb (pre-filled with 0 bytes)
|
||||
ba = byteArray(102400)
|
||||
|
||||
-- Lingo uses garbage-collection, so allocated memory is released when no more references exist.
|
||||
-- For the above variable ba, this can be achieved by calling:
|
||||
ba = VOID
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
Module Checkit {
|
||||
Buffer Clear Mem1 as Byte*12345
|
||||
Print Len(Mem1)
|
||||
Hex Mem1(0) ' print in Hex address of first element
|
||||
Print Mem1(Len(Mem1)-1)-Mem1(0)+1=12345
|
||||
Buffer Mem1 as Byte*20000 ' redim block
|
||||
Print Mem1(Len(Mem1)-1)-Mem1(0)+1=20000
|
||||
Try {
|
||||
Print Mem1(20000) ' it is an error
|
||||
}
|
||||
Print Error$ ' return message: Buffer Locked, wrong use of pointer
|
||||
}
|
||||
Checkit
|
||||
7
Task/Memory-allocation/MATLAB/memory-allocation.m
Normal file
7
Task/Memory-allocation/MATLAB/memory-allocation.m
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
A = zeros(1000); % allocates memory for a 1000x1000 double precision matrix.
|
||||
clear A; % deallocates memory
|
||||
|
||||
b = zeros(1,100000); % pre-allocate memory to improve performance
|
||||
for k=1:100000,
|
||||
b(k) = 5*k*k-3*k+2;
|
||||
end
|
||||
1
Task/Memory-allocation/Maple/memory-allocation-1.maple
Normal file
1
Task/Memory-allocation/Maple/memory-allocation-1.maple
Normal file
|
|
@ -0,0 +1 @@
|
|||
a := Array( 1 .. 10^6, datatype = integer[1] ):
|
||||
1
Task/Memory-allocation/Maple/memory-allocation-2.maple
Normal file
1
Task/Memory-allocation/Maple/memory-allocation-2.maple
Normal file
|
|
@ -0,0 +1 @@
|
|||
unassign( a ):
|
||||
1
Task/Memory-allocation/Maple/memory-allocation-3.maple
Normal file
1
Task/Memory-allocation/Maple/memory-allocation-3.maple
Normal file
|
|
@ -0,0 +1 @@
|
|||
a := 'a':
|
||||
20
Task/Memory-allocation/Maxima/memory-allocation.maxima
Normal file
20
Task/Memory-allocation/Maxima/memory-allocation.maxima
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* Maxima allocates memory dynamically and uses a garbage collector.
|
||||
Here is how to check available memory */
|
||||
|
||||
room();
|
||||
3221/3221 72.3% 2 CONS RATIO COMPLEX STRUCTURE
|
||||
272/307 61.6% FIXNUM SHORT-FLOAT CHARACTER RANDOM-STATE READTABLE SPICE
|
||||
226/404 90.8% SYMBOL STREAM
|
||||
1/2 37.2% PACKAGE
|
||||
127/373 44.9% ARRAY HASH-TABLE VECTOR BIT-VECTOR PATHNAME CCLOSURE CLOSURE
|
||||
370/370 49.1% 1 STRING
|
||||
325/440 8.2% CFUN BIGNUM LONG-FLOAT
|
||||
31/115 98.9% SFUN GFUN VFUN AFUN CFDATA
|
||||
1188/1447 contiguous (478 blocks)
|
||||
11532 hole
|
||||
5242 5.0% relocatable
|
||||
4573 pages for cells
|
||||
22535 total pages
|
||||
97138 pages available
|
||||
11399 pages in heap but not gc'd + pages needed for gc marking
|
||||
131072 maximum pages
|
||||
17
Task/Memory-allocation/Nanoquery/memory-allocation.nanoquery
Normal file
17
Task/Memory-allocation/Nanoquery/memory-allocation.nanoquery
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import native
|
||||
|
||||
// allocate 26 bytes
|
||||
ptr = native.allocate(26)
|
||||
|
||||
// store the uppercase alphabet
|
||||
for i in range(0, 25)
|
||||
native.poke(ptr + i, ord("A") + i)
|
||||
end
|
||||
|
||||
// output the allocated memory
|
||||
for i in range(0, 25)
|
||||
print chr(native.peek(ptr + i))
|
||||
end
|
||||
|
||||
// free the allocated memory
|
||||
native.free(ptr)
|
||||
18
Task/Memory-allocation/Nim/memory-allocation.nim
Normal file
18
Task/Memory-allocation/Nim/memory-allocation.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Allocate thread local heap memory
|
||||
var a = alloc(1000)
|
||||
dealloc(a)
|
||||
|
||||
# Allocate memory block on shared heap
|
||||
var b = allocShared(1000)
|
||||
deallocShared(b)
|
||||
|
||||
# Allocate and Dellocate a single int on the thread local heap
|
||||
var p = create(int, sizeof(int)) # allocate memory
|
||||
# create zeroes memory; createU does not.
|
||||
echo p[] # 0
|
||||
p[] = 123 # assign a value
|
||||
echo p[] # 123
|
||||
discard resize(p, 0) # deallocate it
|
||||
# p is now invalid. Let's set it to nil
|
||||
p = nil # set pointer to nil
|
||||
echo isNil(p) # true
|
||||
3
Task/Memory-allocation/Objeck/memory-allocation.objeck
Normal file
3
Task/Memory-allocation/Objeck/memory-allocation.objeck
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
foo := Object->New(); // allocates an object on the heap
|
||||
foo_array := Int->New[size]; // allocates an integer array on the heap
|
||||
x := 0; // allocates an integer on the stack
|
||||
8
Task/Memory-allocation/Odin/memory-allocation.odin
Normal file
8
Task/Memory-allocation/Odin/memory-allocation.odin
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package main
|
||||
|
||||
import "core:mem"
|
||||
|
||||
main :: proc() {
|
||||
ptr := mem.alloc(1000) // Allocate heap memory
|
||||
mem.free(ptr)
|
||||
}
|
||||
34
Task/Memory-allocation/OxygenBasic/memory-allocation.basic
Normal file
34
Task/Memory-allocation/OxygenBasic/memory-allocation.basic
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
'ALLOCATING MEMORY FROM DIFFERENT MEMORY SOURCES
|
||||
|
||||
sys p
|
||||
|
||||
|
||||
static byte b[0x1000] 'global memory
|
||||
p=@b
|
||||
|
||||
|
||||
function f()
|
||||
local byte b[0x1000] 'stack memory in a procedure
|
||||
p=@b
|
||||
end function
|
||||
|
||||
|
||||
p=getmemory 0x1000 'heap memory
|
||||
...
|
||||
freememory p 'to disallocate
|
||||
|
||||
|
||||
sub rsp,0x1000 'stack memory direct
|
||||
p=rsp
|
||||
...
|
||||
rsp=p 'to disallocate
|
||||
|
||||
|
||||
'Named Memory shared between processes is
|
||||
'also available using the Windows API (kernel32.dll)
|
||||
'see MSDN:
|
||||
'CreateFileMapping
|
||||
'OpenFileMapping
|
||||
'MapViewOfFile
|
||||
'UnmapViewOfFile
|
||||
'CloseHandle
|
||||
1
Task/Memory-allocation/PARI-GP/memory-allocation.parigp
Normal file
1
Task/Memory-allocation/PARI-GP/memory-allocation.parigp
Normal file
|
|
@ -0,0 +1 @@
|
|||
allocatemem(100<<20)
|
||||
12
Task/Memory-allocation/PL-I/memory-allocation-1.pli
Normal file
12
Task/Memory-allocation/PL-I/memory-allocation-1.pli
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
mainproc: proc options(main) reorder;
|
||||
|
||||
subproc: proc;
|
||||
dcl subvar char init ('X');
|
||||
|
||||
put skip data(subvar);
|
||||
subvar = 'Q';
|
||||
end subproc;
|
||||
|
||||
call subproc();
|
||||
call subproc();
|
||||
end mainproc;
|
||||
17
Task/Memory-allocation/PL-I/memory-allocation-2.pli
Normal file
17
Task/Memory-allocation/PL-I/memory-allocation-2.pli
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
mainproc: proc options(main) reorder;
|
||||
dcl ctlvar char ctl;
|
||||
|
||||
alloc ctlvar;
|
||||
ctlvar = 'A';
|
||||
alloc ctlvar;
|
||||
ctlvar = 'B';
|
||||
alloc ctlvar;
|
||||
ctlvar = 'C';
|
||||
|
||||
put skip data(ctlvar);
|
||||
free ctlvar;
|
||||
put skip data(ctlvar);
|
||||
free ctlvar;
|
||||
put skip data(ctlvar);
|
||||
free ctlvar;
|
||||
end mainproc;
|
||||
27
Task/Memory-allocation/PL-I/memory-allocation-3.pli
Normal file
27
Task/Memory-allocation/PL-I/memory-allocation-3.pli
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
mainproc: proc options(main) reorder;
|
||||
dcl list_ptr ptr init (sysnull());
|
||||
dcl list_top ptr init (sysnull());
|
||||
dcl list_end ptr init (addr(list_top));
|
||||
dcl i fixed bin (31);
|
||||
|
||||
dcl 1 list based(list_ptr),
|
||||
2 list_nxt ptr init (sysnull()),
|
||||
2 list_data fixed bin (31) init (i);
|
||||
|
||||
/*
|
||||
* Allocate the list
|
||||
*/
|
||||
do i = 1 to 4;
|
||||
alloc list;
|
||||
list_end -> list_nxt = list_ptr;
|
||||
list_end = list_ptr;
|
||||
end;
|
||||
|
||||
/*
|
||||
* Print the list
|
||||
*/
|
||||
do list_ptr = list_top repeat list_nxt
|
||||
while(list_ptr ^= sysnull());
|
||||
put skip list(list_data);
|
||||
end;
|
||||
end mainprog;
|
||||
9
Task/Memory-allocation/Pascal/memory-allocation-1.pas
Normal file
9
Task/Memory-allocation/Pascal/memory-allocation-1.pas
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
type
|
||||
TByteArray = array of byte;
|
||||
var
|
||||
A: TByteArray;
|
||||
begin
|
||||
setLength(A,1000);
|
||||
...
|
||||
setLength(A,0);
|
||||
end;
|
||||
11
Task/Memory-allocation/Pascal/memory-allocation-2.pas
Normal file
11
Task/Memory-allocation/Pascal/memory-allocation-2.pas
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
type
|
||||
Tcl = class
|
||||
dummy: longint;
|
||||
end;
|
||||
var
|
||||
c1: cl;
|
||||
begin
|
||||
c1:=Tcl.create;
|
||||
...
|
||||
c1.destroy;
|
||||
end;
|
||||
7
Task/Memory-allocation/Phix/memory-allocation.phix
Normal file
7
Task/Memory-allocation/Phix/memory-allocation.phix
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
-->
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">addr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">512</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- limit is 1,610,612,728 bytes on 32-bit systems</span>
|
||||
<span style="color: #0000FF;">...</span>
|
||||
<span style="color: #7060A8;">free</span><span style="color: #0000FF;">(</span><span style="color: #000000;">addr</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">addr2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">512</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- automatically freed when addr2 drops out of scope or re-assigned</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">addr3</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate_string</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"a string"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- automatically freed when addr3 drops out of scope or re-assigned</span>
|
||||
<!--
|
||||
15
Task/Memory-allocation/PureBasic/memory-allocation.basic
Normal file
15
Task/Memory-allocation/PureBasic/memory-allocation.basic
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
*buffer=AllocateMemory(20)
|
||||
|
||||
*newBuffer = ReAllocateMemory(*buffer, 2000) ;increase size of buffer
|
||||
;*buffer value is still valid if newBuffer wasn't able to be reallocated
|
||||
If *newBuffer <> 0
|
||||
*buffer = *newBuffer : *newBuffer = 0
|
||||
EndIf
|
||||
|
||||
FreeMemory(*buffer)
|
||||
|
||||
|
||||
size=20
|
||||
; allocate an image for use with image functions
|
||||
CreateImage(1,size,size)
|
||||
FreeImage(1)
|
||||
15
Task/Memory-allocation/Python/memory-allocation.py
Normal file
15
Task/Memory-allocation/Python/memory-allocation.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
>>> from array import array
|
||||
>>> argslist = [('l', []), ('c', 'hello world'), ('u', u'hello \u2641'),
|
||||
('l', [1, 2, 3, 4, 5]), ('d', [1.0, 2.0, 3.14])]
|
||||
>>> for typecode, initializer in argslist:
|
||||
a = array(typecode, initializer)
|
||||
print a
|
||||
del a
|
||||
|
||||
|
||||
array('l')
|
||||
array('c', 'hello world')
|
||||
array('u', u'hello \u2641')
|
||||
array('l', [1, 2, 3, 4, 5])
|
||||
array('d', [1.0, 2.0, 3.1400000000000001])
|
||||
>>>
|
||||
6
Task/Memory-allocation/R/memory-allocation.r
Normal file
6
Task/Memory-allocation/R/memory-allocation.r
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
x=numeric(10) # allocate a numeric vector of size 10 to x
|
||||
rm(x) # remove x
|
||||
|
||||
x=vector("list",10) #allocate a list of length 10
|
||||
x=vector("numeric",10) #same as x=numeric(10), space allocated to list vector above now freed
|
||||
rm(x) # remove x
|
||||
1
Task/Memory-allocation/REXX/memory-allocation-1.rexx
Normal file
1
Task/Memory-allocation/REXX/memory-allocation-1.rexx
Normal file
|
|
@ -0,0 +1 @@
|
|||
Axtec_god.3='Quetzalcoatl ("feathered serpent"), god of learning, civilization, regeneration, wind and storms'
|
||||
3
Task/Memory-allocation/REXX/memory-allocation-2.rexx
Normal file
3
Task/Memory-allocation/REXX/memory-allocation-2.rexx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
drop xyz NamesRoster j k m caves names. Axtec_god. Hopi Hopi
|
||||
|
||||
/* it's not considered an error to DROP a variable that isn't defined.*/
|
||||
11
Task/Memory-allocation/Racket/memory-allocation.rkt
Normal file
11
Task/Memory-allocation/Racket/memory-allocation.rkt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#lang racket
|
||||
(collect-garbage) ; This function forces a garbage collection
|
||||
|
||||
(current-memory-use) ;Gives an estimate on the memory use based on the last garbage collection
|
||||
|
||||
(custodian-require-memory <limit-custodian>
|
||||
<amount>
|
||||
<stop-custodian>) ; Registers a check on required memory for the <limit-custodian>
|
||||
; If amount of bytes can't be reached, <stop-custodian> is shutdown
|
||||
|
||||
(custodian-limit-memory <custodian> <amount>) ; Register a limit on memory for the <custodian>
|
||||
1
Task/Memory-allocation/Raku/memory-allocation.raku
Normal file
1
Task/Memory-allocation/Raku/memory-allocation.raku
Normal file
|
|
@ -0,0 +1 @@
|
|||
my $buffer = Buf.new(0 xx 1024);
|
||||
29
Task/Memory-allocation/Retro/memory-allocation.retro
Normal file
29
Task/Memory-allocation/Retro/memory-allocation.retro
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
display total memory available
|
||||
|
||||
~~~
|
||||
EOM n:put
|
||||
~~~
|
||||
|
||||
display unused memory
|
||||
|
||||
~~~
|
||||
EOM here - n:put
|
||||
~~~
|
||||
|
||||
display next free address
|
||||
|
||||
~~~
|
||||
here n:put
|
||||
~~~
|
||||
|
||||
allocate 1000 cells
|
||||
|
||||
~~~
|
||||
#1000 allot
|
||||
~~~
|
||||
|
||||
free 500 cells
|
||||
|
||||
~~~
|
||||
#-500 allot
|
||||
~~~
|
||||
2
Task/Memory-allocation/Ring/memory-allocation.ring
Normal file
2
Task/Memory-allocation/Ring/memory-allocation.ring
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
cVar = " " # create variable contains string of 5 bytes
|
||||
cVar = NULL # destroy the 5 bytes string !
|
||||
6
Task/Memory-allocation/Ruby/memory-allocation.rb
Normal file
6
Task/Memory-allocation/Ruby/memory-allocation.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class Thingamajig
|
||||
def initialize
|
||||
fail 'not yet implemented'
|
||||
end
|
||||
end
|
||||
t = Thingamajig.allocate
|
||||
17
Task/Memory-allocation/Rust/memory-allocation.rust
Normal file
17
Task/Memory-allocation/Rust/memory-allocation.rust
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// we have to use `unsafe` here because
|
||||
// we will be dereferencing a raw pointer
|
||||
unsafe {
|
||||
use std::alloc::{Layout, alloc, dealloc};
|
||||
// define a layout of a block of memory
|
||||
let int_layout = Layout::new::<i32>();
|
||||
|
||||
// memory is allocated here
|
||||
let ptr = alloc(int_layout);
|
||||
|
||||
// let us point to some data
|
||||
*ptr = 123;
|
||||
assert_eq!(*ptr, 123);
|
||||
|
||||
// deallocate `ptr` with associated layout `int_layout`
|
||||
dealloc(ptr, int_layout);
|
||||
}
|
||||
1
Task/Memory-allocation/SNOBOL4/memory-allocation-1.sno
Normal file
1
Task/Memory-allocation/SNOBOL4/memory-allocation-1.sno
Normal file
|
|
@ -0,0 +1 @@
|
|||
newstring = "This is creating and saving" " a new single string " "starting with three separate strings."
|
||||
1
Task/Memory-allocation/SNOBOL4/memory-allocation-2.sno
Normal file
1
Task/Memory-allocation/SNOBOL4/memory-allocation-2.sno
Normal file
|
|
@ -0,0 +1 @@
|
|||
newarray = array(100)
|
||||
1
Task/Memory-allocation/SNOBOL4/memory-allocation-3.sno
Normal file
1
Task/Memory-allocation/SNOBOL4/memory-allocation-3.sno
Normal file
|
|
@ -0,0 +1 @@
|
|||
newtable = table()
|
||||
1
Task/Memory-allocation/SNOBOL4/memory-allocation-4.sno
Normal file
1
Task/Memory-allocation/SNOBOL4/memory-allocation-4.sno
Normal file
|
|
@ -0,0 +1 @@
|
|||
data("listnode(next,prev,datafield1,datafield2)")
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue