diff --git a/email/message.go b/email/message.go index 4d096c3..1781935 100644 --- a/email/message.go +++ b/email/message.go @@ -12,9 +12,9 @@ package email import ( "fmt" + "sync" "git.erbosoft.com/amy/amsterdam/config" - "git.erbosoft.com/amy/amsterdam/util" "github.com/CloudyKit/jet/v6" ) @@ -51,7 +51,7 @@ type amMessage struct { } // freeMessages is a free list for amMessage structures. -var freeMessages util.FreeList[amMessage] +var freeMessages sync.Pool // formatAddress outputs an E-mail address with optional name associated with it. func formatAddress(addr string, name string) string { @@ -126,7 +126,7 @@ func (m *amMessage) Send() { * The new Message. */ func AmNewEmailMessage(sender int32, ip string) Message { - rc := freeMessages.Get() + rc := freeMessages.Get().(*amMessage) if rc == nil { rc = &amMessage{to: make([]string, 0), cc: make([]string, 0), bcc: make([]string, 0), headers: make(map[string]string), vars: make(jet.VarMap)} diff --git a/ui/amcontext.go b/ui/amcontext.go index 70c4d01..a145d25 100644 --- a/ui/amcontext.go +++ b/ui/amcontext.go @@ -18,6 +18,7 @@ import ( "net/http" "net/url" "strconv" + "sync" "time" "git.erbosoft.com/amy/amsterdam/config" @@ -510,7 +511,7 @@ var defoptions *AmSessionOptions = &AmSessionOptions{ } // freeContext is a free list for amContext structures. -var freeContext util.FreeList[amContext] +var freeContext sync.Pool // amContextRecycleBin is the channel we put contexts on to be recycled. var amContextRecycleBin chan *amContext @@ -523,7 +524,7 @@ var amContextRecycleBin chan *amContext * Standard Go error status. */ func newContext(ctxt echo.Context) (*amContext, error) { - rc := freeContext.Get() + rc := freeContext.Get().(*amContext) if rc == nil { rc = &amContext{ rendervars: make(jet.VarMap), diff --git a/util/freelist.go b/util/freelist.go deleted file mode 100644 index b41c4a4..0000000 --- a/util/freelist.go +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Amsterdam Web Communities System - * Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -// Package util contains utility definitions. -package util - -import "sync" - -// freeListElem is an element of the free list. -type freeListElem[T any] struct { - next *freeListElem[T] - prev *freeListElem[T] - data *T -} - -// FreeList defines a free list. -type FreeList[T any] struct { - New func() *T - mutex sync.Mutex - listptr *freeListElem[T] -} - -// Put adds a value to the free list. -func (l *FreeList[T]) Put(value *T) { - l.mutex.Lock() - defer l.mutex.Unlock() - ne := freeListElem[T]{data: value} - if l.listptr == nil { - ne.next = &ne - ne.prev = &ne - l.listptr = &ne - } else { - ne.next = l.listptr - ne.prev = l.listptr.prev - ne.next.prev = &ne - ne.prev.next = &ne - } -} - -// Get removes a value from the free list. If there are no values and New is specified, is calls that. -func (l *FreeList[T]) Get() *T { - l.mutex.Lock() - defer l.mutex.Unlock() - var rc *T = nil - if l.listptr == nil { - if l.New != nil { - rc = l.New() - } - } else { - elt := l.listptr - rc = elt.data - l.listptr = elt.next - if l.listptr == elt { - l.listptr = nil - } else { - elt.prev.next = elt.next - elt.next.prev = elt.prev - } - } - return rc -}