Revision history for KeyPgOpNewOverload


Revision [23735]

Last edited on 2019-11-07 02:05:48 by fxm [suppressed useless 'Randomize' in example #1]

No Differences

Revision [22666]

Edited on 2019-05-03 01:39:38 by fxm [Clarified that New [] is one-dimensional]
Additions:
##**New[] Overload**## operator is the (one-dimensional) array-version of the ##**New Overload**## operator and overloads the dynamic memory allocation process provided by the ##**New[] Expression**## operator when applying to a UDT (User Defined Type).
Deletions:
##**New[] Overload**## operator is the array-version of the ##**New Overload**## operator and overloads the dynamic memory allocation process provided by the ##**New[] Expression**## operator when applying to a UDT (User Defined Type).


Revision [22551]

Edited on 2019-01-13 18:50:59 by JeffMarshall [example updated from repo]
Additions:
Dim As Byte a(0 To 10 * 1024 * 1024 - 1) '' 10 megabyte fixed array
Declare Operator New (ByVal size As UInteger) As Any Ptr
Declare Operator Delete (ByVal buffer As Any Ptr)
Operator UDT.New (ByVal size As UInteger) As Any Ptr
Dim pOrig As Any Ptr = CAllocate(ALIGN-1 + SizeOf(UDT Ptr) + size)
Dim pMin As Any Ptr = pOrig + SizeOf(UDT Ptr)
Dim p As Any Ptr = pMin + ALIGN-1 - (CUInt(pMin + ALIGN-1) Mod ALIGN)
Operator UDT.Delete (ByVal buffer As Any Ptr)
End Destructor
If I <> index OrElse Sgn(sign) > 0 Then
Dim As UDTmanager Ptr pu1 = New UDTmanager[cuint(Rnd() * 256 + 1)]
Dim As UDTmanager Ptr pu2 = New UDTmanager[cuint(Rnd() * 256 + 1)]
Dim As UDTmanager Ptr pu = New UDTmanager[cuint(Rnd() * 512 + 1)]
Deletions:
Dim As Byte a(0 to 10 * 1024 * 1024 - 1) '' 10 megabyte fixed array
Declare Operator New (Byval size As UInteger) As Any Ptr
Declare Operator Delete (Byval buffer As Any Ptr)
Operator UDT.New (Byval size As UInteger) As Any Ptr
Dim pOrig As Any Ptr = Callocate(ALIGN-1 + Sizeof(UDT Ptr) + size)
Dim pMin As Any Ptr = pOrig + Sizeof(UDT Ptr)
Dim p As Any Ptr = pMin + ALIGN-1 - (Culng(pMin + ALIGN-1) Mod ALIGN)
Operator UDT.Delete (Byval buffer As Any Ptr)
End destructor
If I <> index Orelse Sgn(sign) > 0 Then
Dim As UDTmanager Ptr pu1 = New UDTmanager[Rnd() * 256 + 1]
Dim As UDTmanager Ptr pu2 = New UDTmanager[Rnd() * 256 + 1]
Dim As UDTmanager Ptr pu = New UDTmanager[Rnd() * 512 + 1]


Revision [22484]

Edited on 2018-09-14 06:52:16 by fxm [Rewording]
Additions:
Memory allocated with ##**New Overload**## operator must be freed by also defining a ##[[KeyPgOpDeleteOverload|Delete Overload]]## operator. Memory allocated with ##**New[] Overload**## operator must be freed by also defining a ##**Delete[] Overload**## operator, the array-version of ##[[KeyPgOpDeleteOverload|Delete Overload]]## operator. You cannot mix and match the different versions of the operators.
Deletions:
Memory allocated with ##**New Overload**## operator must be freed with ##[[KeyPgOpDeleteOverload|Delete Overload]]## operator. Memory allocated with ##**New[] Overload**## operator must be freed with ##**Delete[] Overload**## operator, the array-version of ##[[KeyPgOpDeleteOverload|Delete Overload]]## operator. You cannot mix and match the different versions of the operators.


Revision [22482]

Edited on 2018-09-05 04:21:42 by fxm [Added much simpler example before others (only for syntax usage)]
Additions:
Dynamic allocation displayer for UDT, by using the member operators "New([]) Overload" and "Delete([]) Overload" (very simple example, only for syntax usage):
- displaying of memory allocations: addresses, sizes,
- displaying of memory deallocations: addresses.
{{fbdoc item="filename" value="examples/manual/udt/newoverload0.bas"}}
Type UDTdisplayer
'' display fields:
Declare Operator New (ByVal size As UInteger) As Any Ptr
Declare Operator Delete (ByVal buf As Any Ptr)
Declare Static Function allocation (Byref N As String, Byval size As Uinteger) As Any Ptr
Declare Static Sub deallocation (Byref D As String, Byval p As Any Ptr)
Operator UDTdisplayer.New (ByVal size As UInteger) As Any Ptr
Return UDTdisplayer.allocation("New", size)
Operator UDTdisplayer.Delete (ByVal buf As Any Ptr)
UDTdisplayer.deallocation("Delete", buf)
Operator UDTdisplayer.New[] (ByVal size As UInteger) As Any Ptr
Return UDTdisplayer.allocation("New[]", size)
Operator UDTdisplayer.Delete[] (ByVal buf As Any Ptr)
UDTdisplayer.deallocation("Delete[]", buf)
Function UDTdisplayer.allocation (Byref N As String, Byval size As Uinteger) As Any Ptr
Print "memory allocation for " & size & " bytes from '" & N & "' at address: " & p
Return p
Sub UDTdisplayer.deallocation (Byref D As String, Byval p As Any Ptr)
Print "memory deallocation from '" & D & "' at address " & p
Deallocate p
Dim As UDTdisplayer Ptr pu1 = New UDTdisplayer
Dim As UDTdisplayer Ptr pu2 = New UDTdisplayer[3]
Delete pu1
memory allocation for 1048576 bytes from 'New' at address: 32677920
memory allocation for 3145728 bytes from 'New[]' at address: 33775648
memory deallocation from 'Delete' at address 32677920
memory deallocation from 'Delete[]' at address 33775648


Revision [22470]

Edited on 2018-09-03 15:07:53 by fxm [Added a second example]
Additions:
Print "Message",, "Address" & Space(SizeOf(Any Ptr)), "Size", "Total"
Deletions:
Print "Message",, "Address"& Space(SizeOf(Any Ptr)), "Size", "Total"


Revision [22469]

Edited on 2018-09-03 13:28:01 by fxm [Added a second example]
Additions:
Dim As Byte b(1 To 1024*1024)
Declare Operator New[] (ByVal size As UInteger) As Any Ptr
Declare Operator Delete[] (ByVal buf As Any Ptr)
Static As UInteger maxmemory
Static As UInteger bytes()
Static upbound As UInteger
Declare Static Function printLine (ByRef text As String, ByVal index As UInteger, ByVal sign As Integer) As UInteger
Dim As UInteger UDTmanager.maxmemory = 3 * 1024 * 1024 * 1024
ReDim UDTmanager.address(0)
ReDim UDTmanager.bytes(0)
Dim UDTmanager.upbound As UInteger = 0
Function UDTmanager.printLine (ByRef text As String, ByVal index As UInteger, ByVal sign As Integer) As UInteger
For I As UInteger = 1 To UDTmanager.upbound
Print text, "&h" & Hex(UDTmanager.address(index), SizeOf(Any Ptr) * 2),
ReDim Preserve UDTmanager.address(UDTmanager.upbound)
ReDim Preserve UDTmanager.bytes(UDTmanager.upbound)
Operator UDTmanager.New[] (ByVal size As UInteger) As Any Ptr
ReDim Preserve UDTmanager.address(UDTmanager.upbound)
ReDim Preserve UDTmanager.bytes(UDTmanager.upbound)
Operator UDTmanager.Delete[] (ByVal buf As Any Ptr)
Dim As UInteger found = 0
For I As UInteger = 1 To UDTmanager.upbound
For J As UInteger = I + 1 To UDTmanager.upbound
ReDim Preserve UDTmanager.address(UDTmanager.upbound)
ReDim Preserve UDTmanager.bytes(UDTmanager.upbound)
Print "Message",, "Address"& Space(SizeOf(Any Ptr)), "Size", "Total"
Deletions:
Dim As Byte b(1 to 1024*1024)
Declare Operator New[] (Byval size As Uinteger) As Any Ptr
Declare Operator Delete[] (Byval buf As Any Ptr)
Static As Uinteger maxmemory
Static As Uinteger bytes()
Static upbound As Uinteger
Declare Static Function printLine (Byref text As String, Byval index As Uinteger, Byval sign As Integer) As Uinteger
Dim As Uinteger UDTmanager.maxmemory = 3 * 1024 * 1024 * 1024
Redim UDTmanager.address(0)
Redim UDTmanager.bytes(0)
Dim UDTmanager.upbound As Uinteger = 0
Function UDTmanager.printLine (Byref text As String, Byval index As Uinteger, Byval sign As Integer) As Uinteger
For I As Uinteger = 1 To UDTmanager.upbound
Print text, "&h";
Print Hex(UDTmanager.address(index), Sizeof(Any Ptr) * 2),
Redim Preserve UDTmanager.address(UDTmanager.upbound)
Redim Preserve UDTmanager.bytes(UDTmanager.upbound)
Operator UDTmanager.New[] (Byval size As Uinteger) As Any Ptr
Redim Preserve UDTmanager.address(UDTmanager.upbound)
Redim Preserve UDTmanager.bytes(UDTmanager.upbound)
Operator UDTmanager.Delete[] (Byval buf As Any Ptr)
Dim As Uinteger found = 0
For I As Uinteger = 1 To UDTmanager.upbound
For J As Uinteger = I + 1 To UDTmanager.upbound
Redim Preserve UDTmanager.address(UDTmanager.upbound)
Redim Preserve UDTmanager.bytes(UDTmanager.upbound)
Print "Message",, "Address"& Space(Sizeof(Any Ptr)), "Size", "Total"


Revision [22468]

Edited on 2018-09-03 11:20:20 by fxm [Added a second example]
Additions:
Print Using " +####.## MB"; Sgn(sign) * Cast(Integer, UDTmanager.bytes(index) / 1024) / 1024;
Deletions:
Print Using " +####.## MB"; Cast(Integer, UDTmanager.bytes(index) / 1024) / 1024;


Revision [22467]

Edited on 2018-09-03 10:16:24 by fxm [Added a second example]
Additions:
If I <> index Orelse Sgn(sign) > 0 Then
Print Hex(UDTmanager.address(index), Sizeof(Any Ptr) * 2),
Print Using " +####.## MB"; Cast(Integer, UDTmanager.bytes(index) / 1024) / 1024;
Print "Message",, "Address"& Space(Sizeof(Any Ptr)), "Size", "Total"
Deletions:
If I <> index Orelse Sgn(sign) = +1 Then
#ifdef __FB_64BIT__
Print Hex(UDTmanager.address(index), 16),
#else
Print Hex(UDTmanager.address(index), 8),
#endif
Print Using " +####.## MB"; Sgn(sign) * Cast(Integer, UDTmanager.bytes(index) / 1024) / 1024;
#ifdef __FB_64BIT__
Print "Message",, "Address",, "Size", "Total"
#else
Print "Message",, "Address", "Size", "Total"
#endif


Revision [22464]

Edited on 2018-09-03 09:27:49 by fxm [Added a second example]
Additions:
Print Using " +####.## MB"; Sgn(sign) * Cast(Integer, UDTmanager.bytes(index) / 1024) / 1024;
Deletions:
Print Using " +####.## MB"; Sgn(sign) * Cast(Integer, UDTmanager.bytes(index) / 1024 / 1024);


Revision [22463]

Edited on 2018-09-03 08:06:23 by fxm [Added a second example]
Additions:
Here, program is stopped because of total allocated memory size > 3 GB (adjustable threshold):
Deletions:
Here, program is stopped because of total allocated memory size > 3 GB:


Revision [22462]

Edited on 2018-09-03 08:02:33 by fxm [Added a second example]
Additions:
- detection of a failed allocation (Allocate() returning null pointer),
Deletions:
- Detection of a failed allocation (Allocate() returning null pointer).


Revision [22461]

Edited on 2018-09-03 08:00:56 by fxm [Added a second example]
Additions:
{{fbdoc item="filename" value="examples/manual/udt/newoverload1.bas"}}
%%(freebasic)
%%
Dynamic allocation manager for UDT, by using the member operators "New[] Overload" and "Delete[] Overload":
- monitoring of memory allocations/deallocations: addresses, sizes and total memory used,
- detection of abnormal deallocation requests,
- Detection of a failed allocation (Allocate() returning null pointer).
- detection of total allocated memory size exceeding a threshold,
- the last two detection cases induces an automatic memory freeing before forcing the program to end.
The principle is to manage a dynamic list of successful allocations, but not yet freed, containing the allocated addresses with their requested sizes:
{{fbdoc item="filename" value="examples/manual/udt/newoverload2.bas"}}
%%(freebasic)
Type UDTmanager
'' user UDT fields:
Dim As Byte b(1 to 1024*1024)
'' manager fields:
Public:
Declare Operator New[] (Byval size As Uinteger) As Any Ptr
Declare Operator Delete[] (Byval buf As Any Ptr)
Static As Uinteger maxmemory
Private:
Static As Any Ptr address()
Static As Uinteger bytes()
Static upbound As Uinteger
Declare Static Function printLine (Byref text As String, Byval index As Uinteger, Byval sign As Integer) As Uinteger
Declare Static Sub endProgram ()
Dim As Uinteger UDTmanager.maxmemory = 3 * 1024 * 1024 * 1024
Redim UDTmanager.address(0)
Redim UDTmanager.bytes(0)
Dim UDTmanager.upbound As Uinteger = 0
Function UDTmanager.printLine (Byref text As String, Byval index As Uinteger, Byval sign As Integer) As Uinteger
Dim As UInteger total = 0
For I As Uinteger = 1 To UDTmanager.upbound
If I <> index Orelse Sgn(sign) = +1 Then
total += UDTmanager.bytes(I)
End If
Next I
Print text, "&h";
#ifdef __FB_64BIT__
Print Hex(UDTmanager.address(index), 16),
#else
Print Hex(UDTmanager.address(index), 8),
#endif
If sign <> 0 Then
Print Using " +####.## MB"; Sgn(sign) * Cast(Integer, UDTmanager.bytes(index) / 1024 / 1024);
Else
Print Using "( ####.## MB)"; UDTmanager.bytes(index) / 1024 / 1024;
End If
Print,
Print Using "###.## GB"; total / 1024 / 1024 / 1024
Return total
End Function
Sub UDTmanager.endProgram ()
Do While UDTmanager.upbound > 0
Deallocate UDTmanager.address(UDTmanager.upbound)
UDTmanager.printLine("memory deallocation forced", UDTmanager.upbound, -1)
UDTmanager.upbound -= 1
Redim Preserve UDTmanager.address(UDTmanager.upbound)
Redim Preserve UDTmanager.bytes(UDTmanager.upbound)
Loop
Print "end program forced"
Print
Sleep
End
End Sub
Operator UDTmanager.New[] (Byval size As Uinteger) As Any Ptr
Dim As Any Ptr p = Allocate(size)
If p > 0 Then
UDTmanager.upbound += 1
Redim Preserve UDTmanager.address(UDTmanager.upbound)
Redim Preserve UDTmanager.bytes(UDTmanager.upbound)
UDTmanager.address(UDTmanager.upbound) = p
UDTmanager.bytes(UDTmanager.upbound) = size
If UDTmanager.printLine("memory allocation", UDTmanager.upbound, +1) > UDTmanager.maxmemory Then
UDTmanager.address(0) = p
UDTmanager.bytes(0) = size
Print
UDTmanager.printLine("memory allocation exceeded", 0, 0)
UDTmanager.endProgram()
End If
Return p
Else
UDTmanager.address(0) = p
UDTmanager.bytes(0) = size
Print
UDTmanager.printLine("memory allocation failed", 0, 0)
UDTmanager.endProgram()
End If
Operator UDTmanager.Delete[] (Byval buf As Any Ptr)
Dim As Uinteger found = 0
For I As Uinteger = 1 To UDTmanager.upbound
If UDTmanager.address(I) = buf Then
Deallocate buf
UDTmanager.printLine("memory deallocation", I, -1)
For J As Uinteger = I + 1 To UDTmanager.upbound
UDTmanager.address(J - 1) = UDTmanager.address(J)
UDTmanager.bytes(J - 1) = UDTmanager.bytes(J)
Next J
UDTmanager.upbound -= 1
Redim Preserve UDTmanager.address(UDTmanager.upbound)
Redim Preserve UDTmanager.bytes(UDTmanager.upbound)
found = 1
Exit For
End If
Next I
If found = 0 Then
UDTmanager.address(0) = buf
UDTmanager.bytes(0) = 0
UDTmanager.printLine("deallocation not matching", 0, 0)
End If
#ifdef __FB_64BIT__
Print "Message",, "Address",, "Size", "Total"
#else
Print "Message",, "Address", "Size", "Total"
#endif
Print
Randomize
Dim As UDTmanager Ptr pu1 = New UDTmanager[Rnd() * 256 + 1]
Dim As UDTmanager Ptr pu2 = New UDTmanager[Rnd() * 256 + 1]
Dim As UDTmanager Ptr pu3 = Cast(UDTmanager Ptr, 1)
Delete[] pu2
Delete[] pu3
Delete[] pu2
Delete[] pu1
Do
Dim As UDTmanager Ptr pu = New UDTmanager[Rnd() * 512 + 1]
Loop
Output for fbc 32-bit (maximum dynamic data < 2 GB).
Here, program is stopped because of memory allocation failed:
%%
Message Address Size Total
memory allocation &h020E0020 +99.00 MB 0.10 GB
memory allocation &h083F3020 +3.00 MB 0.10 GB
memory deallocation &h083F3020 -3.00 MB 0.10 GB
deallocation not matching &h00000001 ( 0.00 MB) 0.10 GB
deallocation not matching &h083F3020 ( 0.00 MB) 0.10 GB
memory deallocation &h020E0020 -99.00 MB 0.00 GB
memory allocation &h020ED020 +103.00 MB 0.10 GB
memory allocation &h087F2020 +106.00 MB 0.20 GB
memory allocation &h0F20D020 +230.00 MB 0.43 GB
memory allocation &h1D812020 +137.00 MB 0.56 GB
memory allocation &h2612C020 +377.00 MB 0.93 GB
memory allocation &h3DA30020 +275.00 MB 1.20 GB
memory allocation &h4ED40020 +220.00 MB 1.41 GB
memory allocation &h5C958020 +229.00 MB 1.64 GB
memory allocation failed &h00000000 ( 142.00 MB) 1.64 GB
memory deallocation forced &h5C958020 -229.00 MB 1.41 GB
memory deallocation forced &h4ED40020 -220.00 MB 1.20 GB
memory deallocation forced &h3DA30020 -275.00 MB 0.93 GB
memory deallocation forced &h2612C020 -377.00 MB 0.56 GB
memory deallocation forced &h1D812020 -137.00 MB 0.43 GB
memory deallocation forced &h0F20D020 -230.00 MB 0.20 GB
memory deallocation forced &h087F2020 -106.00 MB 0.10 GB
memory deallocation forced &h020ED020 -103.00 MB 0.00 GB
end program forced
Output for fbc 64-bit (maximum dynamic data < virtual memory).
Here, program is stopped because of total allocated memory size > 3 GB:
%%
Message Address Size Total
memory allocation &h0000000001EA5040 +105.00 MB 0.10 GB
memory allocation &h00000000087BC040 +93.00 MB 0.19 GB
memory deallocation &h00000000087BC040 -93.00 MB 0.10 GB
deallocation not matching &h0000000000000001 ( 0.00 MB) 0.10 GB
deallocation not matching &h00000000087BC040 ( 0.00 MB) 0.10 GB
memory deallocation &h0000000001EA5040 -105.00 MB 0.00 GB
memory allocation &h0000000001EA1040 +155.00 MB 0.15 GB
memory allocation &h000000000B9BF040 +165.00 MB 0.31 GB
memory allocation &h0000000015ED8040 +382.00 MB 0.69 GB
memory allocation &h000000002DCE7040 +458.00 MB 1.13 GB
memory allocation &h000000004A6FB040 +255.00 MB 1.38 GB
memory allocation &h000000005A607040 +96.00 MB 1.48 GB
memory allocation &h000000006061B040 +426.00 MB 1.89 GB
memory allocation &h000000007FFF9040 +221.00 MB 2.11 GB
memory allocation &h000000008DD03040 +119.00 MB 2.22 GB
memory allocation &h0000000095413040 +147.00 MB 2.37 GB
memory allocation &h000000009E727040 +217.00 MB 2.58 GB
memory allocation &h00000000AC03C040 +334.00 MB 2.91 GB
memory allocation &h00000000C0E4B040 +280.00 MB 3.18 GB
memory allocation exceeded &h00000000C0E4B040 ( 280.00 MB) 3.18 GB
memory deallocation forced &h00000000C0E4B040 -280.00 MB 2.91 GB
memory deallocation forced &h00000000AC03C040 -334.00 MB 2.58 GB
memory deallocation forced &h000000009E727040 -217.00 MB 2.37 GB
memory deallocation forced &h0000000095413040 -147.00 MB 2.22 GB
memory deallocation forced &h000000008DD03040 -119.00 MB 2.11 GB
memory deallocation forced &h000000007FFF9040 -221.00 MB 1.89 GB
memory deallocation forced &h000000006061B040 -426.00 MB 1.48 GB
memory deallocation forced &h000000005A607040 -96.00 MB 1.38 GB
memory deallocation forced &h000000004A6FB040 -255.00 MB 1.13 GB
memory deallocation forced &h000000002DCE7040 -458.00 MB 0.69 GB
memory deallocation forced &h0000000015ED8040 -382.00 MB 0.31 GB
memory deallocation forced &h000000000B9BF040 -165.00 MB 0.15 GB
memory deallocation forced &h0000000001EA1040 -155.00 MB 0.00 GB
end program forced
Deletions:
{{fbdoc item="filename" value="examples/manual/udt/newoverload1.bas"}}%%(freebasic)
%%


Revision [22455]

Edited on 2018-08-31 09:32:12 by fxm [New page created]
Additions:
Sleep


Revision [22451]

Edited on 2018-08-30 14:33:44 by fxm [New page created]
Additions:
{{fbdoc item="filename" value="examples/manual/udt/newoverload1.bas"}}%%(freebasic)
Deletions:
{{fbdoc item="filename" value="examples/manual/udt/operator2.bas"}}%%(freebasic)


Revision [22447]

Edited on 2018-08-30 02:32:11 by fxm [New page created]
Additions:
Member operator to overload dynamic memory allocation process part provided by ##[[KeyPgOpNew|Operator New Expression]]## when applying to a UDT (User Defined Type).
The member operator ##**New Overload**## overloads the dynamic memory allocation process part provided by the ##[[KeyPgOpNew|New Expression]]## operator when applying to a UDT (User Defined Type). So the user can define its own dynamic memory allocation process part.
But after that, the UDT instance construction process part provided by the ##[[KeyPgOpNew|New Expression]]## operator is not modified.
Deletions:
Member operator to overload dynamic memory allocation process provided by ##[[KeyPgOpNew|Operator New Expression]]## when applying to a UDT (User Defined Type).
The member operator ##**New Overload**## overloads the dynamic memory allocation process provided by the ##[[KeyPgOpNew|New Expression]]## operator when applying to a UDT (User Defined Type). So the user can define its own dynamic memory allocation process.
But after that, the UDT instance construction process provided by the ##[[KeyPgOpNew|New Expression]]## operator is not modified.


Revision [22444]

Edited on 2018-08-30 02:00:52 by fxm [New page created]
Additions:
Member operators ##**New Overload**##, and ##**New[] Overload**## are always static, even if not explicitly declared (##[[KeyPgStaticMember|static]]## keyword is unnecessary but allowed). Thus, they do not have an implicit ##[[KeyPgThis|This]]## instance argument passed to them (because instance not yet been constructed).
Deletions:
Member operators ##**New Overload**##, and ##**New[] Overload**## are always static, even if not explicitly declared (##[[KeyPgStaticMember|static]]## keyword is unnecessary but allowed). Thus, they do not have an implicit ##[[KeyPgThis|This]]## instance argument passed to them (because instance not yet constructed).


Revision [22435]

Edited on 2018-08-29 13:32:11 by fxm [New page created]
Additions:
Member operator to overload dynamic memory allocation process provided by ##[[KeyPgOpNew|Operator New Expression]]## when applying to a UDT (User Defined Type).
Deletions:
Member operator to overload the dynamic memory allocation process provided by the ##[[KeyPgOpNew|Operator New Expression]]## when applying to a UDT (User Defined Type).


Revision [22421]

Edited on 2018-08-29 08:17:27 by fxm [New page created]
Deletions:
==={{color text="Update in progress (@fxm)" c="red"}}===


Revision [22418]

Edited on 2018-08-29 08:04:20 by fxm [Update in progress]
Additions:
Member operators ##**New Overload**##, and ##**New[] Overload**## are always static, even if not explicitly declared (##[[KeyPgStaticMember|static]]## keyword is unnecessary but allowed). Thus, they do not have an implicit ##[[KeyPgThis|This]]## instance argument passed to them (because instance not yet constructed).
Deletions:
Member operators ##**New Overload**##, and ##**New[] Overload**## are always static, even if not explicitly declared (##[[KeyPgStaticMember|static]]## keyword is unnecessary but allowed).


Revision [22417]

Edited on 2018-08-29 07:48:08 by fxm [Update in progress]

No Differences

Revision [22415]

Edited on 2018-08-29 07:46:14 by fxm [Update in progress]
Additions:
Member operators ##**New Overload**##, and ##**New[] Overload**## are always static, even if not explicitly declared (##[[KeyPgStaticMember|static]]## keyword is unnecessary but allowed).


Revision [22402]

Edited on 2018-08-29 06:25:48 by fxm [Update in progress]
Additions:
- ##[[KeyPgAllocate|Allocate]]##


Revision [22395]

Edited on 2018-08-29 04:02:21 by fxm [Update in progress]
Additions:
Member operator to overload the dynamic memory allocation process provided by the ##[[KeyPgOpNew|Operator New Expression]]## when applying to a UDT (User Defined Type).
Deletions:
Member operator to overload the dynamic memory allocation process provided by the Operator New Expression when applying to a UDT (User Defined Type).


Revision [22388]

Edited on 2018-08-29 03:37:07 by fxm [Update in progress]
Additions:
Member operator to overload the dynamic memory allocation process provided by the Operator New Expression when applying to a UDT (User Defined Type).
The member operator ##**New Overload**## overloads the dynamic memory allocation process provided by the ##[[KeyPgOpNew|New Expression]]## operator when applying to a UDT (User Defined Type). So the user can define its own dynamic memory allocation process.
But after that, the UDT instance construction process provided by the ##[[KeyPgOpNew|New Expression]]## operator is not modified.
##**New[] Overload**## operator is the array-version of the ##**New Overload**## operator and overloads the dynamic memory allocation process provided by the ##**New[] Expression**## operator when applying to a UDT (User Defined Type).
Memory allocated with ##**New Overload**## operator must be freed with ##[[KeyPgOpDeleteOverload|Delete Overload]]## operator. Memory allocated with ##**New[] Overload**## operator must be freed with ##**Delete[] Overload**## operator, the array-version of ##[[KeyPgOpDeleteOverload|Delete Overload]]## operator. You cannot mix and match the different versions of the operators.
Aligned memory allocator:
- by using the member operators "New Overload" and "Delete Overload", any created User object is aligned to a multiple of "ALIGN" bytes (256 bytes in this example),
- the real pointer of the allocated memory is saved just above the User pointer, in the padding block.
{{fbdoc item="filename" value="examples/manual/udt/operator2.bas"}}%%(freebasic)
Const ALIGN = 256
Type UDT
Dim As Byte a(0 to 10 * 1024 * 1024 - 1) '' 10 megabyte fixed array
Declare Operator New (Byval size As UInteger) As Any Ptr
Declare Operator Delete (Byval buffer As Any Ptr)
Declare Constructor ()
Declare Destructor ()
End Type
Operator UDT.New (Byval size As UInteger) As Any Ptr
Print " Overloaded New operator, with parameter size = &h" & Hex(size)
Dim pOrig As Any Ptr = Callocate(ALIGN-1 + Sizeof(UDT Ptr) + size)
Dim pMin As Any Ptr = pOrig + Sizeof(UDT Ptr)
Dim p As Any Ptr = pMin + ALIGN-1 - (Culng(pMin + ALIGN-1) Mod ALIGN)
Cast(Any Ptr Ptr, p)[-1] = pOrig
Operator = p
Print " real pointer = &h" & Hex(pOrig), "return pointer = &h" & Hex(p)
End Operator
Operator UDT.Delete (Byval buffer As Any Ptr)
Print " Overloaded Delete operator, with parameter buffer = &h" & Hex(buffer)
Dim pOrig As Any Ptr = Cast(Any Ptr Ptr, buffer)[-1]
Deallocate(pOrig)
Print " real pointer = &h" & Hex(pOrig)
End Operator
Constructor UDT ()
Print " Constructor, @This = &h" & Hex(@This)
End Constructor
Destructor UDT ()
Print " Destructor, @This = &h" & Hex(@This)
End destructor
Print "'Dim As UDT Ptr p = New UDT'"
Dim As UDT Ptr p = New UDT
Print " p = &h" & Hex(p)
Print "'Delete p'"
Delete p
Output example:
%%
'Dim As UDT Ptr p = New UDT'
Overloaded New operator, with parameter size = &hA00000
real pointer = &h420020 return pointer = &h420100
Constructor, @This = &h420100
p = &h420100
'Delete p'
Destructor, @This = &h420100
Overloaded Delete operator, with parameter buffer = &h420100
real pointer = &h420020
- ##[[KeyPgOpNew|New Expression]]##
- ##[[KeyPgOpDeleteOverload|Delete Overload]]##
Deletions:
Member operator to overload the dynamic allocation process provided by the Operator New Expression when applying to a UDT (User Defined Type).
The member operator ##**New Overload**## overloads the dynamic allocation process provided by the ##[[KeyPgOpNew|Operator New Expression]]## when applying to a UDT (User Defined Type). So the user can define its own dynamic allocation process.
But after that, the UDT instance construction process provided by the ##[[KeyPgOpNew|Operator New Expression]]## is not modified.
For simple types, like integers, an initial value can be given. For types without constructors, initial values can be specified for each field (either with default initializer at data-field declaration, or with initializer list as in ##**New** //datatype// (**initializers, ..**)## if all type data-fields are numeric primitives only and without any default initializers). For types with at least one constructor, the initialize list (if any) must match an existing constructor. If no initializers are given, the default values for those types will be set.
##**New[]**## is the array-version of the ##**New**## operator and allocates enough memory for the specified number of objects. The default constructor for the type will be used to set the initial values for each item.
Objects created with ##**New**## must be freed with ##[[KeyPgOpDelete|Delete]]##. Memory allocated with ##**New[]**## must be freed with ##**Delete[]**##, the array-version of ##[[KeyPgOpDelete|Delete]]##. You cannot mix and match the different versions of the operators.
Specifying an initial value of ##[[KeyPgAny|Any]]##, as in ##**New** //datatype// (**Any**)## will allocate memory for the type, but not initialize the data. This is only valid on data types that do not have constructors (otherwise for data types with constructors, syntax of simple memory allocation with pointer conversion, like //Cptr(datatype Ptr, Allocate(Sizeof(datatype)))//, can be substituted to the invalid use of New...Any).
Specifying an initial value of ##[[KeyPgAny|Any]]##, as in ##**New** //datatype//[//count//] {**Any**}## will allocate memory for the array, but not initialize the data. This is only valid on data types that do not have constructors (otherwise for data types with constructors, syntax of simple memory allocation with pointer conversion, like //Cptr(datatype Ptr, Allocate(count * Sizeof(datatype)))//, can be substituted to the invalid use of New...Any).
This operator can be overloaded for user-defined types as a member ##[[KeyPgOperator|Operator]]##.
**Note:** Using ##//pointer// = **New** //datatype//[//count//]## may be unsafe if ##//pointer//## was declared with a type different from ##//datatype//## (for sub-type polymorphism purpose for example), because the pointer arithmetic fails to access the elements if the pointer type size is different from the size of ##//datatype//## (when using ##{{fbdoc item="keyword" value="KeyPgOpPtrIndex|Operator [] (Pointer index)"}}## or adding an offset (element number) to the pointer, or even when ##**Delete[]**## itself (the array-version of ##[[KeyPgOpDelete|Delete]]##) must destroy the elements).
{{fbdoc item="filename" value="examples/manual/operator/new.bas"}}%%(freebasic)
type Rational
as integer numerator, denominator
end type
scope
' Create and initialize a "rational" and store its address.
dim p as Rational ptr = new Rational(3, 4)
print p->numerator & "/" & p->denominator
' Destroy the rational and give its memory back to the system.
delete p
end scope
scope
' Allocate memory for 100 integers and store the address of the first one.
dim p as integer ptr = new integer[100]
' Assign some values to the integers in the array.
for i as integer = 0 to 99
p[i] = i
next
' Free the entire integer array.
delete[] p
end scope
- ##[[KeyPgOpDelete|Delete]]##
- ##[[KeyPgOpPlacementNew|Placement New]]##


Revision [22387]

Edited on 2018-08-29 02:00:03 by fxm [Update in progress]
Additions:
The member operator ##**New Overload**## overloads the dynamic allocation process provided by the ##[[KeyPgOpNew|Operator New Expression]]## when applying to a UDT (User Defined Type). So the user can define its own dynamic allocation process.
But after that, the UDT instance construction process provided by the ##[[KeyPgOpNew|Operator New Expression]]## is not modified.
Deletions:
The member operator ##**New Overload**## overloads the dynamic allocation process provided by the ##[[KeyPgOpNew|Operator New Expression]]## when applying to a UDT (User Defined Type). The UDT instance construction process provided by the ##[[KeyPgOpNew|Operator New Expression]]## is not modified.


Revision [22386]

Edited on 2018-08-29 01:41:24 by fxm [Update in progress]
Additions:
The member operator ##**New Overload**## overloads the dynamic allocation process provided by the ##[[KeyPgOpNew|Operator New Expression]]## when applying to a UDT (User Defined Type). The UDT instance construction process provided by the ##[[KeyPgOpNew|Operator New Expression]]## is not modified.
Deletions:
The member operator ##**New Overload**## overloads the dynamic allocation process provided by the ##[[KeyPgOpNew|Operator New Expression]]## when applying to a UDT (User Defined Type).


Revision [22385]

Edited on 2018-08-29 01:30:46 by fxm [Update in progress]
Additions:
A pointer of type [[KeyPgAny|any]] [[KeyPgPtr|ptr]] to the start of the newly allocated memory.
Deletions:
A pointer of type [[KeyPgAny|any]] [[KeyPgPtr|ptr]] to the newly allocated data.


Revision [22384]

Edited on 2018-08-29 01:16:21 by fxm [Update in progress]
Additions:
A pointer of type [[KeyPgAny|any]] [[KeyPgPtr|ptr]] to the newly allocated data.
The member operator ##**New Overload**## overloads the dynamic allocation process provided by the ##[[KeyPgOpNew|Operator New Expression]]## when applying to a UDT (User Defined Type).
Deletions:
{{fbdoc item="usage"}}##
//result// = **New** //[[DataType|datatype]]//
//or//
//result// = **New** //[[DataType|datatype]]// ( //initializers//, ... )
//or//
//result// = **New** //[[DataType|datatype]]//**[** //count// **]**
##//initializers//##
Initial value(s) for the variable.
##//datatype//##
Name of the data type to create.
##//count//##
Exact number of elements to allocate.
A pointer of type [[DataType|datatype]] to the newly allocated data.
The ##**New**## operator dynamically allocates memory and constructs a specified data type.


Revision [22381]

Edited on 2018-08-29 00:54:33 by fxm [Update in progress]
Additions:
Member operator to overload the dynamic allocation process provided by the Operator New Expression when applying to a UDT (User Defined Type).
Deletions:
Operator to dynamically allocate memory and construct data of a specified type.


Revision [22380]

Edited on 2018-08-29 00:24:54 by fxm [Update in progress]
Additions:
{{fbdoc item="title" value="Operator New Overload"}}----
==={{color text="Update in progress (@fxm)" c="red"}}===
Deletions:
{{fbdoc item="title" value="Operator New"}}----
==={{color text="Update in progress (@fxm)." c="red"}}===


Revision [22377]

The oldest known version of this page was created on 2018-08-29 00:20:06 by fxm [Update in progress]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode