summary refs log tree commit diff stats
path: root/lib/system/alloc.nim
Commit message (Collapse)AuthorAgeFilesLines
* fix allocator corruption for large sizes (#7338)jcosborn2018-03-171-36/+45
| | | | | | * fix allocator corruption for large sizes * allow large chunks to coalesce and added test case * use correct constants in MaxBigChunkSize
* make the allocator take a special path for allocations bigger than 2GB; ↵Araq2018-02-271-2/+26
| | | | fixes #7120
* fixes the tracking of 'occupied memory'Andreas Rumpf2018-01-301-6/+96
|
* alloc.nim: Make 'logAlloc' produce a toy Nim program for easier memory tracingAndreas Rumpf2018-01-181-2/+2
|
* GC improvements; distinguish between thread local and globals in the marking ↵Araq2018-01-141-4/+4
| | | | step
* allocator: minor fix for deallocOsPagesAndreas Rumpf2017-12-111-0/+1
|
* make the new allocator workAraq2017-12-071-8/+15
|
* make allocator use the TLSF algorithm; work in progressAraq2017-12-071-30/+127
|
* fixes #3558Araq2017-10-161-7/+8
|
* fixes withRegion for --gc:stackAndreas Rumpf2017-06-191-1/+0
|
* memory manager: use less memory; corruption preventionAndreas Rumpf2017-04-021-59/+47
|
* Fixes #4719. (#5585)Eugene Kabanov2017-03-231-0/+3
|
* hotfix: allocator: don't set origSize to 0Araq2017-03-151-2/+2
|
* make memory tracker work without onThreadCreationAraq2017-03-141-4/+13
|
* introduce nimMaxHeap define to fight memory overcommitAndreas Rumpf2017-02-101-0/+5
|
* system.nim: don't use deprecated symbols/constructsAraq2017-02-081-1/+1
|
* critical realloc bugfix; refs #4818Andreas Rumpf2017-02-031-3/+4
|
* allocator: fixes regression: get alignment right for small objectsAraq2017-01-311-0/+2
|
* make AVL tree node part of the memory regions; fixes hard to reproduce ↵Araq2017-01-311-15/+15
| | | | channel crashes
* fix the racy allocator properlyAndreas Rumpf2017-01-311-1/+1
|
* added test case; threadex example crashes nowAraq2017-01-301-1/+5
|
* fixes #5301Araq2017-01-301-40/+55
|
* improvements to memtrackingAndreas Rumpf2017-01-261-1/+7
|
* memory allocator hotfix: do not allocate tremendous amounts of memoryAndreas Rumpf2017-01-131-11/+13
|
* disable new alloctor behaviour for emscriptenAndreas Rumpf2016-12-271-4/+5
|
* fixes #4818Araq2016-12-181-2/+22
|
* further memtracking improvementsAndreas Rumpf2016-11-241-0/+6
|
* bugfix: bottom of AVL tree is now threadsafeAndreas Rumpf2016-09-241-2/+2
|
* clean up a few stray c_stdout's in gc debug codeJacek Sieka2016-07-311-3/+3
|
* fix types of ansi_c/sysio to more closely match C ABIJacek Sieka2016-06-051-2/+2
| | | | | also fixes some instances of using C library functions when there are nim alternatives available
* fixes #3184Andreas Rumpf2016-05-221-29/+32
|
* beginnings of --gc:stackAndreas Rumpf2016-03-121-147/+1
|
* further progress on --gc:v2Andreas Rumpf2016-02-171-4/+4
|
* Use ByteAddress instead of deprecated TAddressdef2016-01-241-1/+1
|
* lambda lifting support for iterToProc pluginAraq2015-12-011-1/+4
|
* Merge remote-tracking branch 'nim-lang/devel' into emscripten-supportAndrey Sobolev2015-10-011-4/+7
|\
| * Add option to disable munmap() use in the allocator.Reimer Behrends2015-09-181-4/+7
| | | | | | | | | | | | When compiling with '-d:nimAllocNoUnmap', the allocator will not attempt to return large chunks to the OS. For certain allocation behaviors, this can be a significant speedup.
* | mmap non PageSize aligned supportAndrey Sobolev2015-09-101-1/+48
|/ | | | mmap non PageSize aligned support
* proper distinction between --gc:none and --os:standaloneAraq2015-06-291-9/+24
|
* Fix typos in commentsdef2015-06-211-1/+1
|
* lib/system/a-e - Dropped 'T' from typespdw2015-06-041-90/+94
|
* Use more Natural and Positive numbers in proc parametersdef2015-04-061-102/+102
| | | | | - Didn't go through all modules, only the main ones I thought of - Building the compiler and tests still work
* some better sysassert messagesSimon Hafner2015-04-041-2/+2
|
* introduced 'benign' pragmaAraq2014-10-251-1/+1
|
* renamefestAraq2014-08-231-2/+2
|
* system files use new identifiersAraq2014-08-231-35/+35
|
* the big renamefest: first stepsAraq2014-08-221-2/+2
|
* added getTotalSharedMem et al.Jostein Berre Eliassen2014-05-201-0/+16
|
* first version of 'spawn'Araq2014-04-161-2/+5
|
* improvements for 'pretty'Araq2013-12-281-3/+3
|
a9042a58bc6c9595a0'>e5bd3b5b9 ^
c7158af75 ^



19bc408ec ^
c7158af75 ^


e5bd3b5b9 ^
a22c1f444 ^
19bc408ec ^
e5bd3b5b9 ^
fdde4d3a9 ^
e5bd3b5b9 ^



9cc23118b ^
e5bd3b5b9 ^
e424e13bd ^



42e6130b2 ^






e424e13bd ^
fdde4d3a9 ^


e424e13bd ^





f5db2de69 ^



42e6130b2 ^
b5b5e6e76 ^
e424e13bd ^


b5b5e6e76 ^
e424e13bd ^





42e6130b2 ^
b5b5e6e76 ^


42e6130b2 ^
e424e13bd ^
740527813 ^
fdde4d3a9 ^
f317807a8 ^
ce23b814a ^




9c6de08db ^
ce23b814a ^


5b0d8246f ^
9c6de08db ^
ce23b814a ^
9c6de08db ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302


                               
                                         





                                                       
                       
 
                                             



                                                















                                                                           
  

                                        





                                                      

                                              
                                    

                   
  


                                                    

                        

                          
                           
                                                                
                       
                            




                                                    
                            

                                                                           





                                                                   
                   






















                                                                             




                                                            












                                                      

                          



                                                             














                                                                                     

             

                                   










                                                      



                                                                   
                               

                                                     



                                   





                                                                 
                      

                                                                   
                                                         

                            




                                              
 
                                                                               
                                                                      
              
 



























                                                                            
 


                                                                   
                                  



                                                                           



                                                                        
                                                                   


                                                                   
                 
                                         
                                                               
                                      
 



                                                          
                                  
    



                                                          






                                                                
                                                                           


                                                  





                                                         



                                                                               
                                        
                                                      


                                                                             
                                     





                                                                          
                                                         


                                                       
                                                             
       
                                        
 
                                                                           




                                                                               
                         


                                          
                                                          
                                                                               
                                                           



                                                              
#
#
#           The Nimrod Compiler
#        (c) Copyright 2013 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module implements semantic checking for calls. 
# included from sem.nim

proc sameMethodDispatcher(a, b: PSym): bool =
  result = false
  if a.kind == skMethod and b.kind == skMethod: 
    var aa = lastSon(a.ast)
    var bb = lastSon(b.ast)
    if aa.kind == nkSym and bb.kind == nkSym:
      if aa.sym == bb.sym: 
        result = true
    else:
      nil
      # generics have no dispatcher yet, so we need to compare the method
      # names; however, the names are equal anyway because otherwise we
      # wouldn't even consider them to be overloaded. But even this does
      # not work reliably! See tmultim6 for an example:
      # method collide[T](a: TThing, b: TUnit[T]) is instantiated and not
      # method collide[T](a: TUnit[T], b: TThing)! This means we need to
      # *instantiate* every candidate! However, we don't keep more than 2-3
      # candidated around so we cannot implement that for now. So in order
      # to avoid subtle problems, the call remains ambiguous and needs to
      # be disambiguated by the programmer; this way the right generic is
      # instantiated.
  
proc determineType(c: PContext, s: PSym)

proc pickBestCandidate(c: PContext, headSymbol: PNode,
                       n, orig: PNode,
                       initialBinding: PNode,
                       filter: TSymKinds,
                       best, alt: var TCandidate,
                       errors: var seq[string]) =
  var o: TOverloadIter
  var sym = initOverloadIter(o, c, headSymbol)
  var symScope = o.lastOverloadScope

  var z: TCandidate
  
  if sym == nil: return
  initCandidate(best, sym, initialBinding, symScope)
  initCandidate(alt, sym, initialBinding, symScope)
  best.state = csNoMatch
  
  while sym != nil:
    if sym.kind in filter:
      determineType(c, sym)
      initCandidate(z, sym, initialBinding, o.lastOverloadScope)
      z.calleeSym = sym
      matches(c, n, orig, z)
      if errors != nil:
        errors.safeAdd(getProcHeader(sym))
        if z.errors != nil:
          for err in z.errors:
            errors[errors.len - 1].add("\n  " & err)
      if z.state == csMatch:
        # little hack so that iterators are preferred over everything else:
        if sym.kind == skIterator: inc(z.exactMatches, 200)
        case best.state
        of csEmpty, csNoMatch: best = z
        of csMatch:
          var cmp = cmpCandidates(best, z)
          if cmp < 0: best = z   # x is better than the best so far
          elif cmp == 0: alt = z # x is as good as the best so far
          else: nil
    sym = nextOverloadIter(o, c, headSymbol)

proc NotFoundError*(c: PContext, n: PNode, errors: seq[string]) =
  # Gives a detailed error message; this is separated from semOverloadedCall,
  # as semOverlodedCall is already pretty slow (and we need this information
  # only in case of an error).
  if c.InCompilesContext > 0: 
    # fail fast:
    GlobalError(n.info, errTypeMismatch, "")
  var result = msgKindToString(errTypeMismatch)
  add(result, describeArgs(c, n, 1 + ord(nfDelegate in n.flags)))
  add(result, ')')
  
  var candidates = ""
  for err in errors:
    add(candidates, err)
    add(candidates, "\n")

  if candidates != "":
    add(result, "\n" & msgKindToString(errButExpected) & "\n" & candidates)

  LocalError(n.Info, errGenerated, result)

proc gatherUsedSyms(c: PContext, usedSyms: var seq[PNode]) =
  for scope in walkScopes(c.currentScope):
    if scope.usingSyms != nil:
      for s in scope.usingSyms: usedSyms.safeAdd(s)

proc resolveOverloads(c: PContext, n, orig: PNode,
                      filter: TSymKinds): TCandidate =
  var initialBinding: PNode
  var alt: TCandidate
  var f = n.sons[0]
  if f.kind == nkBracketExpr:
    # fill in the bindings:
    initialBinding = f
    f = f.sons[0]
  else:
    initialBinding = nil

  var errors: seq[string]
  var usedSyms: seq[PNode]
 
  template pickBest(headSymbol: expr) =
    pickBestCandidate(c, headSymbol, n, orig, initialBinding,
                      filter, result, alt, errors)

  gatherUsedSyms(c, usedSyms)
  if usedSyms != nil:
    var hiddenArg = if usedSyms.len > 1: newNode(nkClosedSymChoice, n.info, usedSyms)
                    else: usedSyms[0]

    n.sons.insert(hiddenArg, 1)
    orig.sons.insert(hiddenArg, 1)
    
    pickBest(f)
 
    if result.state != csMatch:
      n.sons.delete(1)
      orig.sons.delete(1)
    else: return

  pickBest(f)

  let overloadsState = result.state
  if overloadsState != csMatch:
    if nfDelegate in n.flags:
      InternalAssert f.kind == nkIdent
      let calleeName = newStrNode(nkStrLit, f.ident.s)
      calleeName.info = n.info

      let callOp = newIdentNode(idDelegator, n.info)
      n.sons[0..0] = [callOp, calleeName]
      orig.sons[0..0] = [callOp, calleeName]
     
      pickBest(callOp)

    if overloadsState == csEmpty and result.state == csEmpty:
      LocalError(n.info, errUndeclaredIdentifier, considerAcc(f).s)
      return
    elif result.state != csMatch:
      if nfExprCall in n.flags:
        LocalError(n.info, errExprXCannotBeCalled,
                   renderTree(n, {renderNoComments}))
      else:
        errors = @[]
        pickBest(f)
        NotFoundError(c, n, errors)
      return

  if alt.state == csMatch and cmpCandidates(result, alt) == 0 and
      not sameMethodDispatcher(result.calleeSym, alt.calleeSym):
    InternalAssert result.state == csMatch
    #writeMatches(result)
    #writeMatches(alt)
    if c.inCompilesContext > 0: 
      # quick error message for performance of 'compiles' built-in:
      GlobalError(n.Info, errGenerated, "ambiguous call")
    elif gErrorCounter == 0:
      # don't cascade errors
      var args = "("
      for i in countup(1, sonsLen(n) - 1):
        if i > 1: add(args, ", ")
        add(args, typeToString(n.sons[i].typ))
      add(args, ")")

      LocalError(n.Info, errGenerated, msgKindToString(errAmbiguousCallXYZ) % [
        getProcHeader(result.calleeSym), getProcHeader(alt.calleeSym),
        args])


proc instGenericConvertersArg*(c: PContext, a: PNode, x: TCandidate) =
  if a.kind == nkHiddenCallConv and a.sons[0].kind == nkSym and
      isGenericRoutine(a.sons[0].sym):
    let finalCallee = generateInstance(c, a.sons[0].sym, x.bindings, a.info)
    a.sons[0].sym = finalCallee
    a.sons[0].typ = finalCallee.typ
    #a.typ = finalCallee.typ.sons[0]

proc instGenericConvertersSons*(c: PContext, n: PNode, x: TCandidate) =
  assert n.kind in nkCallKinds
  if x.genericConverter:
    for i in 1 .. <n.len:
      instGenericConvertersArg(c, n.sons[i], x)

proc IndexTypesMatch(c: PContext, f, a: PType, arg: PNode): PNode = 
  var m: TCandidate
  initCandidate(m, f)
  result = paramTypesMatch(c, m, f, a, arg, nil)
  if m.genericConverter and result != nil:
    instGenericConvertersArg(c, result, m)

proc ConvertTo*(c: PContext, f: PType, n: PNode): PNode = 
  var m: TCandidate
  initCandidate(m, f)
  result = paramTypesMatch(c, m, f, n.typ, n, nil)
  if m.genericConverter and result != nil:
    instGenericConvertersArg(c, result, m)

proc semResolvedCall(c: PContext, n: PNode, x: TCandidate): PNode =
  assert x.state == csMatch
  var finalCallee = x.calleeSym
  markUsed(n.sons[0], finalCallee)
  if finalCallee.ast == nil:
    internalError(n.info, "calleeSym.ast is nil") # XXX: remove this check!
  if finalCallee.ast.sons[genericParamsPos].kind != nkEmpty:
    # a generic proc!
    if not x.proxyMatch:
      finalCallee = generateInstance(c, x.calleeSym, x.bindings, n.info)
    else:
      result = x.call
      result.sons[0] = newSymNode(finalCallee, result.sons[0].info)
      result.typ = finalCallee.typ.sons[0]
      if ContainsGenericType(result.typ): result.typ = errorType(c)
      return
  result = x.call
  instGenericConvertersSons(c, result, x)
  result.sons[0] = newSymNode(finalCallee, result.sons[0].info)
  result.typ = finalCallee.typ.sons[0]

proc semOverloadedCall(c: PContext, n, nOrig: PNode,
                       filter: TSymKinds): PNode =
  var r = resolveOverloads(c, n, nOrig, filter)
  if r.state == csMatch: result = semResolvedCall(c, n, r)
  # else: result = errorNode(c, n)
    
proc explicitGenericInstError(n: PNode): PNode =
  LocalError(n.info, errCannotInstantiateX, renderTree(n))
  result = n

proc explicitGenericSym(c: PContext, n: PNode, s: PSym): PNode =
  var x: TCandidate
  initCandidate(x, s, n)
  var newInst = generateInstance(c, s, x.bindings, n.info)
  markUsed(n, s)
  result = newSymNode(newInst, n.info)

proc explicitGenericInstantiation(c: PContext, n: PNode, s: PSym): PNode = 
  assert n.kind == nkBracketExpr
  for i in 1..sonsLen(n)-1:
    n.sons[i].typ = semTypeNode(c, n.sons[i], nil)
  var s = s
  var a = n.sons[0]
  if a.kind == nkSym:
    # common case; check the only candidate has the right
    # number of generic type parameters:
    if safeLen(s.ast.sons[genericParamsPos]) != n.len-1:
      let expected = safeLen(s.ast.sons[genericParamsPos])
      LocalError(n.info, errGenerated, "cannot instantiate: " & renderTree(n) &
         "; got " & $(n.len-1) & " type(s) but expected " & $expected)
      return n
    result = explicitGenericSym(c, n, s)
  elif a.kind in {nkClosedSymChoice, nkOpenSymChoice}:
    # choose the generic proc with the proper number of type parameters.
    # XXX I think this could be improved by reusing sigmatch.ParamTypesMatch.
    # It's good enough for now.
    result = newNodeI(a.kind, n.info)
    for i in countup(0, len(a)-1): 
      var candidate = a.sons[i].sym
      if candidate.kind in {skProc, skMethod, skConverter, skIterator}: 
        # if suffices that the candidate has the proper number of generic 
        # type parameters:
        if safeLen(candidate.ast.sons[genericParamsPos]) == n.len-1:
          result.add(explicitGenericSym(c, n, candidate))
    # get rid of nkClosedSymChoice if not ambiguous:
    if result.len == 1 and a.kind == nkClosedSymChoice:
      result = result[0]
    # candidateCount != 1: return explicitGenericInstError(n)
  else:
    result = explicitGenericInstError(n)

proc SearchForBorrowProc(c: PContext, startScope: PScope, fn: PSym): PSym =
  # Searchs for the fn in the symbol table. If the parameter lists are suitable
  # for borrowing the sym in the symbol table is returned, else nil.
  # New approach: generate fn(x, y, z) where x, y, z have the proper types
  # and use the overloading resolution mechanism:
  var call = newNode(nkCall)
  var hasDistinct = false
  call.add(newIdentNode(fn.name, fn.info))
  for i in 1.. <fn.typ.n.len:
    let param = fn.typ.n.sons[i]
    let t = skipTypes(param.typ, abstractVar-{tyTypeDesc})
    if t.kind == tyDistinct or param.typ.kind == tyDistinct: hasDistinct = true
    call.add(newNodeIT(nkEmpty, fn.info, t.baseOfDistinct))
  if hasDistinct:
    var resolved = semOverloadedCall(c, call, call, {fn.kind})
    if resolved != nil:
      result = resolved.sons[0].sym