X-Git-Url: https://ocean-lang.org/code/?p=ocean-D;a=blobdiff_plain;f=Blog-pointers;h=2539232ddae05443b331b93e01e327ac716f7311;hp=59b943f482f3e042322ffb2fca7c80d391a7330a;hb=62fa958bc69d7b1701376c391a80382d7a7d00d7;hpb=18d02a36152f7ab7325e4b82a03d885a6a3bc56c diff --git a/Blog-pointers b/Blog-pointers index 59b943f..2539232 100644 --- a/Blog-pointers +++ b/Blog-pointers @@ -6,7 +6,7 @@ Traditionally (in lanuages like C) pointers are both powerful and dangerous. My goal is to control that power without extinguishing it, so they become powerful with more clarity, and without the danger. -Pointers have always been overloaded - the can store either a pointer +Pointers have always been overloaded - they can store either a pointer to an actual object, or a 'nil' (or 'NULL') pointer, which isn't an object and cannot be dereferenced safely. This provides power (it enables simple finite linked lists) and danger (it is too easy to try @@ -19,7 +19,7 @@ compiler so notation errors are easily caught and corrected. As well as allowing `nil`, it is sometimes useful to allow other non-pointer values to be stored in a pointer object. The Linux kernel -does this a lot, and the are two specific cases that are used. +does this a lot, and there are two specific cases that are used. In the first case, small integers (normally negative) can be stored in a pointer variable. These are used as error codes. As the first page of virtual memory and also the last page are not mapped, any pointer @@ -41,11 +41,11 @@ third, level of "loading" that can be declared for a pointer. The Linux kernel uses these in hashtables that support lockless lookups and movement of entries between chains. The loaded value is treated like a `nil` pointer, but records which hash chain has come to an -end. If a search find the wrong hash value at the end of the chain, +end. If a search finds the wrong hash value at the end of the chain, it know that it might have been diverted between chains and needs to re-check. -The odd pointer values can also be used in a different way. In each +The odd pointer values can also be used in a different way. Instead of indicating a different interpretation of the whole pointer, the least significant bit can be treated as a separate value that is stored in the pointer - to optimize space usage. A particular use for @@ -88,7 +88,7 @@ pattern, but it is not the only valid pattern. Ocean will support this natively by allowing a field on a structure to be identified as reference counter, but it will allow other options as well. -Points can be classified as either "owned" or "borrowed" references. +Pointers can be classified as either "owned" or "borrowed" references. This will probably be determined dynamically from code analysis, though in some cases such as function parameters and returns, it must be declared. An owned reference is one that holds a reference count @@ -114,14 +114,14 @@ it isn't possible to know at time of analysis which owned pointer is the primary, so both (or all) possible primaries must be preserved. The "Scope" of the borrowed reference that the owned reference must be -preserved for will often a a lexical scope, but may not always be. I -imaging that the language may at some stage be able describe other +preserved for will often be a lexical scope, but may not always be. I +imagine that the language may at some stage be able describe other temporal relationships between different objects. In that case, the borrowed reference must have a life time contained within the guaranteed lifetime of the matching borrowed pointer. I should say that "Garbage collection" will be an option, just not the -only or the default. Language can potentially identify exactly the +only or the default. The language can potentially identify exactly the references that need to be checked. Maybe collectable pointers reserve the lsb for mark, prior to sweep. compiler would extract a description of places that gc refs can live, @@ -167,7 +167,7 @@ Rust uses smart pointers to implement others. It even has Box<> to create on heap instead of stack. -Why doesn't I just do that? Partly because I don't have classes yet!! +Why don't I just do that? Partly because I don't have classes yet!! I like a simple syntax to test if a pointer is over-loaded. if pointer? @@ -241,3 +241,30 @@ or better fun foo(a:x, b:x) : x +------------------------ +Years later - March 2021 + +I need somewhere to start so I need to be able to ignore lots of this detail. +So in the first instance all references are counted references. They must refer +to a struct that contains 'refcount'. + +A reference is declared with + name : ref base-type +and the base object can be accessed with + name.ref +though this can sometimes be inferred from "name". +In particular + name.foo +will find 'foo' either as an attribute of name, or of what name refers to. +If name refers to a reference, this recurses. + +A ref can be checked with "name.valid" + +A new object can be allocated with "name.new()", which returns the ref. +So "name.new().valid" is true if the allocation succeeded, which is always +will on Linux. + +Future ideas might include: + type name : ref(attr,list) basetype +where attr,list can include borrow,counted,single, etc +